pandas 在 Plotly 中向条形图添加注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35637889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:45:47  来源:igfitidea点击:

Adding annotations to a bar graph in Plotly

pythonpandasgraphannotationsplotly

提问by Kevin

I'm looking to add some annotations to my bar graph I created in Plotly. I simply want to show the data value over each bar without the need of hovering over it.

我希望为我在 Plotly 中创建的条形图添加一些注释。我只是想在每个条形上显示数据值,而无需将鼠标悬停在它上面。

Most of the data manipulation has been done and my two main data frames have been created:

大部分数据操作已经完成,我的两个主要数据框已经创建:

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

Here's my graph:

这是我的图表:

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>

采纳答案by Kevin

By working off of Sam's answer and referencing 33873298, I was able to create some annotations.

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

Creating the annotations:

创建注释:

# 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

Now you can add "annotations" to your layout variable:

现在您可以向布局变量添加“注释”:

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

Due to the x-axis being dates/ string, I was unable to position the annotations better (e.g. I couldn't take advantage of using the +0.2 and -0.2 utilized in 33873298). The current code I have however solves my problem.

由于 x 轴是日期/字符串,我无法更好地定位注释(例如,我无法利用33873298中使用的 +0.2 和 -0.2 )。然而,我目前的代码解决了我的问题。

回答by Sam

The answer you are looking for is here: https://plot.ly/python/text-and-annotations/

您正在寻找的答案在这里:https: //plot.ly/python/text-and-annotations/

For your code it would be something like:

对于您的代码,它将类似于:

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

If you leave the 'texposition' arg blank, I believe you only get text on the hover-over

如果您将 'texposition' arg 留空,我相信您只会在悬停时获得文本

Edit: I guess the textposition arg does not work for a bar graph. I've nominally tested something like the code below, which should work for a bar.

编辑:我猜 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'])
    ]
)

You can edit the formatting as you see fit. Let me know if this works.

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