【问题标题】:text showing up in hoverinfo not just displayedhoverinfo 中显示的文本不仅显示
【发布时间】:2021-12-17 21:18:36
【问题描述】:

所以我正在尝试添加数据标签,以便您在查看图表时可以看到我的每个堆栈的值。我添加了文本选项并放置了我想要显示的列,但它只是在悬停信息中返回,而不仅仅是显示在图表上。我该如何更改?

df2 = pd.DataFrame.from_dict({'Country': {0: 'Europe',
  1: 'America',
  2: 'Asia',
  3: 'Europe',
  4: 'America',
  5: 'Asia',
  6: 'Europe',
  7: 'America',
  8: 'Asia',
  9: 'Europe',
  10: 'America',
  11: 'Asia'},
 'Year': {0: 2014,
  1: 2014,
  2: 2014,
  3: 2015,
  4: 2015,
  5: 2015,
  6: 2016,
  7: 2016,
  8: 2016,
  9: 2017,
  10: 2017,
  11: 2017},
 'Amount': {0: 1600,
  1: 410,
  2: 150,
  3: 1300,
  4: 300,
  5: 170,
  6: 1000,
  7: 500,
  8: 200,
  9: 900,
  10: 500,
  11: 210}})


fig = go.Figure()

x=[]
for i in df2['Year'].unique():
    x.append(str(i))

for c in df2['Country'].unique():
    df3 = df2[df2['Country'] == c]
    fig.add_trace(go.Bar(x=x, y=df3['Amount'], name=c, text=df3['Amount']))
fig.update_layout(title="Personnel at Work", 
                  barmode='stack',
                  title_x=.5,
                  yaxis={
                     'showgrid':False,
                     'visible':False
                       },
                  xaxis=dict(
                    tick0=0,
                    dtick=1,
                    ),
                  plot_bgcolor='rgba(0,0,0,0)')
fig.show()

【问题讨论】:

    标签: pandas plotly


    【解决方案1】:

    我遇到了类似的问题,这段代码帮助了我!我不确定它是否可以帮助您解决问题,但请尝试一下。

    fig.update_traces(texttemplate='%{your_labels =:.1f}', textposition='outside')
    

    在这里浏览所有用例,

    https://plotly.com/python/text-and-annotations/

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 2014-10-26
      • 2020-08-23
      • 2017-09-21
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多