图文本标签在折线图中不起作用 python

figure text label not working in line chart plotly python

我正在尝试在图表本身上创建带有文本标签的折线图,但它不起作用。 是 使用文本参数在散点图中工作得很好,您可以在下面看到。

代码-

import plotly.express as px

fig = px.scatter(crime2,
                 x='murder',
                 y='burglary',
                 size='population',
                 text='state')  # add figure label
fig.update_layout(xaxis_title='Murder Rate',
                  yaxis_title='Burglary Rate')
fig.update_traces(marker=dict(color='red'))
fig.show()

但它不适用于折线图。 折线图代码 -

fig = px.line(covid_subset,
              x='date',
              y='total_cases',
              color='location',
              text='location'
)
fig.update_layout(title='Cumulative Confirmed covid-19 cases')
fig.show()

如您所见,折线图上的国家/地区标签不是只在一个地方显示,而是一直显示在整条线上。

我可能缺少一些参数,但我不知道它是什么。

这就是我想要做的 -

这实际上是参数 text 的工作原理:始终显示文本出现在给定列上的时间。我想到了两种可能的解决方法,一种使用技巧,另一种使用 annotations。这些都不是理想的解决方案,但您可以从这里开始。

获取数据

import numpy as np
import pandas as pd
import plotly.express as px

url = "https://github.com/owid/covid-19-data/raw/master/public/data/owid-covid-data.csv"
df = pd.read_csv(url)

countries = ["Brazil", "Germany", "India", "Indonesia", "Italy",
             "Mexico", "New Zealand", "Norway", "South Africa",
             "South Korea", "United States"]

df = df[df["location"].isin(countries)].reset_index(drop=True)

注释

grp = df.groupby("location")[["date", "total_cases"]].last().reset_index()

annotations = []
for i, row in grp.iterrows():
    annotations.append(
    dict(x=row["date"],
         y=row["total_cases"],
         text=row["location"],
        xref="x",
        yref="y",
        showarrow=True,
#         arrowhead=7,
        ax=50,
        ay=0
        ))
    
    
fig = px.line(df,
              x='date',
              y='total_cases',
              color='location',
)
fig.update_layout(title='Cumulative Confirmed covid-19 cases',
                  title_x=0.5,
                  showlegend=False,
                  annotations=annotations)
fig.show()

技巧

df["date"] = df["date"].astype("M8")

grp = df.groupby("location")[["date", "total_cases"]]\
        .last().reset_index()
grp["text"] = grp["location"].copy()

grp["date"] = grp["date"] + pd.offsets.DateOffset(days=5)

df1 = pd.concat([df, grp], ignore_index=True, sort=False)\
        .sort_values(["location", "date"]).reset_index(drop=True)

df1["text"] = np.where(df1["text"].isnull(), "", df1["text"])

fig = px.line(df1,
              x='date',
              y='total_cases',
              color='location',
              text="text",
              hover_data={"text":False}
)
fig.update_traces(textposition='top center') 
fig.update_layout(title='Cumulative Confirmed covid-19 cases',
                  title_x=0.5,
                  showlegend=False,)
fig.show()

这里有一个稍微多一点的compact/DRY选择:

import plotly.express as px

df = px.data.gapminder().query("continent == 'Oceania'")

fig = px.line(df, x="year", y="lifeExp", color="country")
fig.for_each_trace(lambda t: fig.add_annotation(
    x=t.x[-1], y=t.y[-1], text=t.name, 
    font_color=t.line.color,
    ax=5, ay=0, xanchor="left", showarrow=False
))
fig.update_layout(showlegend=False)

fig.show()