我在python中使用Plotly来生成数字。与标题一样,我不能用update_annotations函数更新图形注释。
以下是多重绘图的一个示例。
data = pd.DataFrame(np.random.rand(10,3), columns=['A', 'B', 'C'], index=pd.date_range(start='2001-01-01', periods=10))
fig = make_subplots(rows=3, cols=1, subplot_titles=['Top','Middle', 'Bottom'])
fig.add_trace(go.Scatter(x=data.index, y=data['A'], mode='lines'), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['B'], mode='lines'), row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['C'], mode='lines'), row=3, col=1)我可以用下面的代码将顶部图的名称从“top”更改为“TOP_TEST”及其位置。
fig['layout']['annotations'][0]['text'] = 'TOP_TEST'
fig['layout']['annotations'][0]['x'] = 0.02但是,我不明白为什么不能对函数update_annotations做同样的事情。如果它有效,那么一次更改多个参数似乎要容易得多。
fig.update_annotations(row=1, col=1, text='TOP_TEST', x=0.02)谢谢您事先提出的任何意见。
发布于 2022-04-02 10:35:49
_select_annotations_like()row或col参数,内部方法有效地返回空列表。在此之后,代码会变得更具有挑战性。这似乎是一个bugselector参数中使用update_annotations()。在下面的代码中演示
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
data = pd.DataFrame(np.random.rand(10,3), columns=['A', 'B', 'C'], index=pd.date_range(start='2001-01-01', periods=10))
fig = make_subplots(rows=3, cols=1, subplot_titles=['Top','Middle', 'Bottom'])
fig.add_trace(go.Scatter(x=data.index, y=data['A'], mode='lines'), row=1, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['B'], mode='lines'), row=2, col=1)
fig.add_trace(go.Scatter(x=data.index, y=data['C'], mode='lines'), row=3, col=1)
# empty so nothing to update...
list(fig._select_annotations_like(prop="annotations", row=1, col=1))
# select based on text on we're ok
fig.update_annotations(selector={"text":"Top"}, text="TOP_TEST", x=.02)https://stackoverflow.com/questions/71716047
复制相似问题