如何在 plotly python 中为 Y-axis 标题添加工具提示?

How to add tooltip for Y-axis title in plotly python?

我想在子图中为 y-axis 标题添加工具提示,因为标题可能很长。 Please click on this link to see Image

您可以创建假线并将其粘贴到 Y-Axis。由于 scatter 支持悬停,您可以自定义工具提示。这是我的方法:

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig = make_subplots(rows=3, cols=1)


#Trace1
fig.append_trace(go.Scatter(
x=[3.5, 4.5],
y=[1.75, 2.75],
mode='markers'),row=1, col=1)

#Trace1 Fake Y Axis
fig.add_trace(go.Scatter(
x=[0,0,0.01,0.01,0],
y=[0,3.5,3.5,0,0],
fill="toself",
mode='lines',
fillcolor='black',
line=dict(color="black",width=0.1),
text='My Custom Text 11111111111111',
opacity=1), row=1, col=1)


#Trace2
fig.append_trace(go.Scatter(
x=[1.5, 4.5],
y=[0.75, 0.75],
mode='markers'),row=2, col=1)

#Trace2 Fake Y Axis
fig.add_trace(go.Scatter(
x=[0,0,0.01,0.01,0],
y=[0,3.5,3.5,0,0],
fill="toself",
mode='lines',
fillcolor='black',
line=dict(color="black",width=0.1),
text='My Custom Text 22222222222222',
opacity=1), row=2, col=1)


#Trace3
fig.append_trace(go.Scatter(
x=[1.5, 4.5],
y=[0.75, 3.75],
mode='markers'),row=3, col=1)

#Trace3 Fake Y Axis
fig.add_trace(go.Scatter(
x=[0,0,0.01,0.01,0],
y=[0,3.5,3.5,0,0],
fill="toself",
mode='lines',
fillcolor='black',
line=dict(color="black",width=0.1),
text='My Custom Text 33333333333333',
opacity=0), row=3, col=1) # You can make it invisible (opacity=0). Hover on 
                          # still shows text.


fig.update_xaxes(range=[0, 7], showgrid=True)
fig.update_yaxes(range=[0, 3.5])



fig.show()

示例输出: