1 回答

TA贡献1872条经验 获得超3个赞
如果除了线条和详细的 y 轴之外,您对上面的设置感到满意,那么您可以放弃多索引方法,只需在适当的位置使用注释fig.add_annotation()
下图是用下面的代码片段生成的:
使用 , 为左侧的注释留出空间
fig.update_layout(margin=dict(l=150))
,将区间名称和数据存储在字典中,并且
计算每个指定区间的中间值,以及
将注释放置在 y 轴的左侧,使用
xref="paper"
, 和不会弄乱 y 轴刻度线的值。
阴谋
完整代码:
import plotly.graph_objects as go
import numpy as np
x = np.arange(-10,10,1)
y = np.arange(-10,10,1)
y_annotation = [ 'Flexion' if data > 0 else 'Extension' for data in y ]
intervals = {'Flexion':[0,10],
'Extension':[0, -10]}
# plotly setup
fig = go.Figure( data= go.Scatter(x=x,y=y) )
# make room for annotations
fig.update_layout(margin=dict(l=150))
for k in intervals.keys():
fig.add_annotation(dict(font=dict(color="green",size=14),
#x=x_loc,
x=-0.16,
y=(intervals[k][0]+intervals[k][1])/2,
showarrow=False,
text="<i>"+k+"</i>",
textangle=0,
xref="paper",
yref="y"
))
fig.show()
添加回答
举报