在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>

凯文

通过解决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 )。但是,当前的代码可以解决我的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在R代码中向条形图添加SE条形图?

来自分类Dev

如何在Matplotlib中的堆栈百分比条形图上添加注释?

来自分类Dev

向R中的条形图添加分组线

来自分类Dev

如何在ColdFusion中向条形图添加折线?

来自分类Dev

在matplotlib中向条形图添加面板

来自分类Dev

向ggplot中的条形图添加错误条

来自分类Dev

如何从数据框向堆叠的条形图添加自定义注释?

来自分类Dev

熊猫,条形图注释

来自分类Dev

镜像条形图与plotly

来自分类Dev

如何在d3条形图上添加注释数组?

来自分类Dev

如何在Jasper Report中向条形图添加水平线?

来自分类Dev

向ggplot2 R中的堆叠条形图添加误差线-已解决

来自分类Dev

MPAndroidChart-向条形图添加标签

来自分类Dev

如何使用jQuery向条形图添加动画?

来自分类Dev

向x轴条形图添加额外的标签

来自分类Dev

向多水平条形图添加垂直线

来自分类Dev

Python-在子图显示中添加注释

来自分类Dev

尝试向defrecord定义的类中添加注释

来自分类Dev

向父类中定义的字段添加注释

来自分类Dev

如何在Python中向YAML文件添加注释

来自分类Dev

在Eclipse中向断点添加注释或描述

来自分类Dev

如何在VIM中向多行添加注释?

来自分类Dev

如何在 Swift 中向图表添加注释?

来自分类Dev

使用 plotly 使 matplotlib 堆积条形图在 jupyter 中交互

来自分类Dev

向R中的ggplot2中的堆叠条形图添加水平线,并在图例中显示

来自分类Dev

在地层图外添加注释

来自分类Dev

matplotlib:在图外添加注释

来自分类Dev

向MuPDF android添加注释

来自分类Dev

使用perl向pdf添加注释