繁体   English   中英

Plotly:使用辅助 y 轴进行注释

[英]Plotly: Annotate using secondary y-axis

从 plotly拿了一个例子。 并对其进行了修改,以便第二条轨迹绘制在 secondary_y 轴上。 我想使用辅助轴作为参考来注释该线上的一个点。

这是代码:

import plotly.graph_objects as go

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(go.Scatter(
   x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
   y=[0, 11, 31, 21, 41, 31, 41, 61, 51]
))

fig.add_trace(go.Scatter(
   x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
   y=[0, 4, 5, 1, 2, 2, 3, 4, 2]
), secondary_y=True)

fig.add_annotation(
       x=2,
       y=5,
       xref="x",
       yref="y",
       text="max=5",
       showarrow=True,
       font=dict(
           family="Courier New, monospace",
           size=16,
           color="#ffffff"
           ),
       align="center",
       arrowhead=2,
       arrowsize=1,
       arrowwidth=2,
       arrowcolor="#636363",
       ax=20,
       ay=-30,
       bordercolor="#c7c7c7",
       borderwidth=2,
       borderpad=4,
       bgcolor="#ff7f0e",
       opacity=0.8
       )

fig.update_layout()

这样就出来了,注释应该在第二行。 输出,注解不在线

您只需要在fig.add_annotation()中将yref="y"替换为yref="y2" ,请参见下面的示例。

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

fig = make_subplots(specs=[[{"secondary_y": True}]])

fig.add_trace(go.Scatter(
   x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
   y=[0, 11, 31, 21, 41, 31, 41, 61, 51]
))

fig.add_trace(go.Scatter(
   x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
   y=[0, 4, 5, 1, 2, 2, 3, 4, 2]
), secondary_y=True)

fig.add_annotation(
       x=2,
       y=5,
       xref="x",
       yref="y2",
       text="max=5",
       showarrow=True,
       font=dict(
           family="Courier New, monospace",
           size=16,
           color="#ffffff"
           ),
       align="center",
       arrowhead=2,
       arrowsize=1,
       arrowwidth=2,
       arrowcolor="#636363",
       ax=20,
       ay=-30,
       bordercolor="#c7c7c7",
       borderwidth=2,
       borderpad=4,
       bgcolor="#ff7f0e",
       opacity=0.8
       )

fig.show()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM