2016-02-25 334 views
1

我正在为我在Plotly中创建的条形图添加一些注释。我只是想显示每个栏的数据值,而不需要将它悬停在上面。向Plotly中的条形图添加注释

大多数数据操作已经完成,我的两个主要数据帧已创建:

In [1]: df_MMCAlist 
Out[1]: 
    List Date Total 
0 2015-01 131 
1 2015-02 102 
2 2015-03 116 
3 2015-04 104 
4 2015-05 105 
5 2015-06  87 
6 2015-07  68 
7 2015-08  58 
8 2015-09  73 
9 2015-10  76 
10 2015-11  74 
11 2015-12  67 

In [2]:df_MMCAsale 
Out[2]: 
    Sale Date Total 
0 2015-01  70 
1 2015-02  42 
2 2015-03  92 
3 2015-04  36 
4 2015-05  49 
5 2015-06  47 
6 2015-07  34 
7 2015-08  37 
8 2015-09  42 
9 2015-10  39 
10 2015-11  42 
11 2015-12  32 

这里是我的图表:

bar_MMCAlist = go.Bar(
    x=df_MMCAlist['List Date'], 
    y=df_MMCAlist['Total'], 
    name='Listings', 
    marker=dict(
     color='rgb(242,137,17)' 
    ) 
) 

bar_MMCAsale = go.Bar(
    x=df_MMCAsale['Sale Date'], 
    y=df_MMCAsale['Total'], 
    name='Sales', 
    marker=dict(
     color='rgb(0,153,0)' 
    ) 
) 

data = [bar_MMCAlist, bar_MMCAsale] 
layout = go.Layout(
    barmode='group', 
    xaxis=dict(nticks=13), 
) 

fig = go.Figure(data=data, layout=layout) 

plot_url = py.plot(fig, filename = 'Yearly Performance') 

<a href="https://plot.ly/~ryebooks/14/" target="_blank" title="Listings vs Sales" style="display: block; text-align: center;"><img src="https://plot.ly/~ryebooks/14.png" alt="Listings vs Sales" style="max-width: 100%;width: 1620px;" width="1620" onerror="this.onerror=null;this.src='https://plot.ly/404.png';" /></a> 
 
    <script data-plotly="ryebooks:14" src="https://plot.ly/embed.js" async></script>

回答

0

通过处理Sam的回答并引用33873298,我能够创建一些注释。

创建注释:

# Specify the y-position of the labels 
y1 = df_MMCAlist['Total'] 
y2 = df_MMCAsale['Total'] 

xcoord = df_MMCAsale['Sale Date'] # Specify the x-position of the labels 

annotationsList = [dict(
       x=xi, 
       y=yi, 
       text=str(yi), 
       xanchor='right', 
       yanchor='bottom', 
       showarrow=False, 
      ) for xi, yi in zip(xcoord, y1)] 

annotationsSale = [dict(
       x=xi, 
       y=yi, 
       text=str(yi), 
       xanchor='left', 
       yanchor='bottom', 
       showarrow=False, 
      ) for xi, yi in zip(xcoord, y2)] 

annotations = annotationsList + annotationsSale 

现在,你可以在“注释”添加到您的布局变量:

layout = go.Layout(
    barmode='group', 
    annotations=annotations, 
    xaxis=dict(nticks=13), 
) 

由于x轴为日期/串,我无法位置注释更好(例如,我无法利用33873298中使用的+0.2和-0.2)。我现在的代码解决了我的问题。

1

的你要找的答案就在这里: https://plot.ly/python/text-and-annotations/

为您的代码会是这样的:如果你离开“texposition”精氨酸空白

bar_MMCAsale = go.Bar(
    x=df_MMCAsale['Sale Date'], 
    y=df_MMCAsale['Total'], 
    name='Sales', 
    text = df_MMCAsale['Total'], 
    textposition='top right', 
    textfont=dict(
     family='sans serif', 
     size=18, 
     color='#1f77b4' 
    ) 
    marker=dict(
     color='rgb(0,153,0)' 
    ) 
) 

,我相信,你只在鼠标悬停

编辑得到的文本:我猜textposition arg不适用于条形图。我名义上测试了下面的代码,这应该适用于酒吧。

bar_MMCAsale = go.Bar(
    x=df_MMCAsale['Sale Date'], 
    y=df_MMCAsale['Total'], 
    name='Sales', 
    marker=dict(
     color='rgb(0,153,0)' 
    ) 
) 
layout = go.Layout(
    showlegend=False, 
    annotations=[ 
     dict(
      x=xpos, 
      y=ypos, 
      xref='x', 
      yref='y', 
      text=str(ypos), 
      showarrow=True, 
      arrowhead=7, 
      ax=0, 
      ay=-40 
    ) for xpos, ypos in zip(df_MMCAsale['Sale Date'], df_MMCAsale['Total']) 
    ] 
) 

您可以根据需要编辑格式。让我知道这个是否奏效。