Plotly Stacked Bar Chart Annotations

时间:2016-10-18 20:40:56

标签: python annotations bar-chart plotly stacked-chart

我一直在尝试根据教程示例 - Styled Bar Chart with Direct Labels

为叠加的阴谋条形图创建注释

我修改了示例,如下所示:

import plotly.plotly as py
import plotly.graph_objs as go

x1 = ['Product A', 'Product B', 'Product C']
y1 = [20, 14, 23]

x2 = ['Product A', 'Product B', 'Product C']
y2 = [40, 10, 55]

bar1 = go.Bar(
        x=x1,y=y1,
        marker=dict(
            color='rgb(158,202,225)'
        ),
        opacity=0.6,
        name = 'Type 1'
    )

bar2 = go.Bar(
        x=x2,y=y2,
        marker=dict(
            color='rgb(158,202,0)'
        ),
        opacity=0.6,
        name = 'Type 2'
    )

data = [bar1, bar2]

annotation1 = [dict(x=xi,y=yi,
         text=str(yi),
         xanchor='center',
         yanchor='bottom',
         showarrow=False,
    ) for xi, yi in zip(x1, y1)]

annotation2 = [dict(x=xi,y=yi,
         text=str(yi),
         xanchor='center',
         yanchor='bottom',
         showarrow=False,
    ) for xi, yi in zip(x2, y2)]

layout = go.Layout(annotations=annotation1 + annotation2, barmode="stack")

fig = go.Figure(data=data, layout=layout)
url5 = py.plot(fig, filename = 'bar-direct-labels', sharing = 'secret', fileopt = 'overwrite')

创建了这个情节:https://plot.ly/~Viola.Rudenko/918/type-1-vs-type-2/

enter image description here

如何将注释文本与正确的产品类型对齐,以便文本显示在相应的产品类型条形图的中间?我试图将y中的annotation2移到y1 (y = yi+y1),但这没有帮助。

0 个答案:

没有答案