也许不是最优雅的方法,但是下面完整的sn-p会产生下图。 sn-ps 的一些核心部分是:
方法:
for i, d in enumerate(fig.data):
for j, a in enumerate(d.x):
fig.add_annotation(x=a, y = d.y[j], text = str(d.y[j])[:5],
showarrow = False,
yshift = 10,
font=dict(color=d.line.color, size=12))
情节1:
如果您想关注其他 color cycles 的注释,只需包括:
colors = px.colors.qualitative.Alphabet
并替换:
font=dict(color=d.line.color, size=12)
与:
font=dict(color=colors[i], size=12)
得到:
情节2:
如果您可以使用,我很乐意详细介绍所有细节。
完整代码:
# imports
import pandas as pd
import plotly.express as px
# data
df = px.data.stocks().tail(10)
df = df.drop(['AMZN', 'AAPL'], axis = 1)
df.set_index('date', inplace = True)
colors = px.colors.qualitative.Alphabet
fig = px.line(df, x = df.index, y = df.columns)
for i, d in enumerate(fig.data):
for j, a in enumerate(d.x):
fig.add_annotation(x=a, y = d.y[j], text = str(d.y[j])[:5],
showarrow = False,
yshift = 10,
font=dict(color=d.line.color, size=12)
# font=dict(color=colors[i], size=12)
)
fig.show()