I want to create a line chart with multiple lines sharing the same x and y axis. Fortunately, newest version of Plotly seems to allow multiple inputs as y. Example: px.line(df,x,[y1,y2])
works fine.
Unfortunately, I want to add text as datapoint annotations and this does not work in the same way.
My goal is to create a lineplot with multiple lines and each datapoint annotated with its y_value in the same color as the corresponding line.
Ty for your help!
Perhaps not the most elegant approach, but the complete snippet below will produce the following figure. Some central parts of the snippets are:
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))
If you'd like to follow other color cycles for your annotations, just include:
colors = px.colors.qualitative.Alphabet
And replace:
font=dict(color=d.line.color, size=12)
with:
font=dict(color=colors[i], size=12)
And get:
I'd be happy to go into all the details if this is something you could use.
# 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.