【问题标题】:figure text label not working in line chart plotly python图形文本标签在折线图中不起作用 python
【发布时间】:2020-11-13 14:21:18
【问题描述】:

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

代码-

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()

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

我可能遗漏了一些参数,但我不知道它是什么。

这就是我想要做的 -

【问题讨论】:

    标签: plotly plotly-python


    【解决方案1】:

    这里有一个稍微紧凑/干燥的替代方案:

    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()
    

    【讨论】:

    • 不错。但是在这里,拥有像我们的数据世界这样的东西会很棒。我的意思是,如果有很多痕迹,很难区分一个注释和另一个注释。
    • 是的。文本回避是一个非常具有挑战性的问题,需要在 Plotly.js 中处理,但我们可能需要赞助商:目前我们无法独自承担。
    【解决方案2】:

    这实际上是参数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()
    

    【讨论】:

    • 谢谢,非常感谢您从 owid github repo 中提取数据并制作图表来解释它
    猜你喜欢
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2016-01-16
    • 2013-04-24
    • 1970-01-01
    相关资源
    最近更新 更多