【问题标题】:Individually labeled bars for bar graph in PlotlyPlotly 中条形图的单独标记条形图
【发布时间】:2016-02-25 16:37:08
【问题描述】:

我试图为分组条形图创建注释 - 其中每个条形图都有一个特定的数据标签,显示该条形图的值并且位于条形图的中心上方。

我尝试对教程中的示例进行简单的修改来实现这一点,如下:

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

x = ['Product A', 'Product B', 'Product C']
y1 = [20, 14, 23]
y2 = [12, 18, 29]

annotations1 = [dict(
            x=xi,
            y=yi,
            text=str(yi),
            xanchor='auto',
            yanchor='bottom',
            showarrow=False,
        ) for xi, yi in zip(x, y1)]
annotations2 = [dict(
            x=xi,
            y=yi,
            text=str(yi),
            xanchor='auto',
            yanchor='bottom',
            showarrow=False,
        ) for xi, yi in zip(x, y2)]
annotations = annotations1 + annotations2

trace1 = go.Bar(
    x=x,
    y=y1,
    name='SF Zoo'
)
trace2 = go.Bar(
    x=x,
    y=y2,
    name='LA Zoo'
)
data = [trace1, trace2]
layout = go.Layout(
    barmode='group',
    annotations=annotations
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='stacked-bar')

产生这个情节:https://plot.ly/~ashish.baghudana/49.embed

但是,数据标签不是以单个条形为中心,而是在每个条形的中心。我想知道是否有解决方法,而不是手动注释。

【问题讨论】:

    标签: python r bar-chart plotly


    【解决方案1】:

    现在 Plotly 通过其 API 直接支持这一点,通过设置 textposition(在大多数情况下设置为 "auto" 可能就足够了)。

    应用于您的示例:

    import plotly.plotly as py
    import plotly.graph_objs as go
    
    x = ['Product A', 'Product B', 'Product C']
    y1 = [20, 14, 23]
    y2 = [12, 18, 29]
    
    trace1 = go.Bar(
        x=x,
        y=y1,
        text=str(y1),
        textposition='auto',
        name='SF Zoo'
    )
    trace2 = go.Bar(
        x=x,
        y=y2,
        text=str(y2),
        textposition='auto',
        name='LA Zoo'
    )
    data = [trace1, trace2]
    layout = go.Layout(
        barmode='group',
        annotations=annotations
    )
    fig = go.Figure(data=data, layout=layout)
    plot_url = py.plot(fig, filename='stacked-bar')
    

    【讨论】:

      【解决方案2】:

      这有点骇人听闻,但它可以完成工作。

      x = ['Product A', 'Product B', 'Product C']
      y1 = [20, 14, 23]
      y2 = [12, 18, 29]
      
      xcoord = [0,1,2]
      
      annotations1 = [dict(
                  x=xi-0.2,
                  y=yi,
                  text=str(yi),
                  xanchor='auto',
                  yanchor='bottom',
                  showarrow=False,
              ) for xi, yi in zip(xcoord, y1)]
      
      annotations2 = [dict(
                  x=xi+0.2,
                  y=yi,
                  text=str(yi),
                  xanchor='auto',
                  yanchor='bottom',
                  showarrow=False,
              ) for xi, yi in zip(xcoord, y2)]
      
      annotations = annotations1 + annotations2
      

      【讨论】:

        猜你喜欢
        • 2011-01-11
        • 2020-11-28
        • 2013-03-03
        • 1970-01-01
        • 2021-12-28
        • 1970-01-01
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        相关资源
        最近更新 更多