Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Individually labeled bars for bar graph in Plotly

I was trying to create annotations for grouped bar charts - where each bar has a specific data label that shows the value of that bar and is located above the centre of the bar.

I tried a simple modification of the examples in tutorial to achieve this, as follows:

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

Which produces this plot: https://plot.ly/~ashish.baghudana/49.embed Example Image

However,the data labels are not centred over individual bars, but over the centre of each group of bars. I was wondering if there is a workaround to this, rather than annotating manually.

like image 703
ashishbaghudana Avatar asked Nov 23 '15 14:11

ashishbaghudana


People also ask

What are the 3 labels of a bar graph?

Understanding a Bar Graph The bars display the value for a particular category of data. The vertical axis on the left or right side of the bar graph is called the y-axis. The horizontal axis at the bottom of a bar graph is called the x-axis. The height or length of the bars represents the value of the data.


2 Answers

This is slightly hackish, but it gets the job done.

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

Barchart

like image 176
capt-calculator Avatar answered Oct 16 '22 06:10

capt-calculator


By now Plotly directly supports this through its API by setting textposition (to "auto" might suffice in most cases).

Applied to your example:

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')
like image 37
Salim B Avatar answered Oct 16 '22 07:10

Salim B