Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing total on stacked bar Plotly

How do you show the total on a stacked bar graph in Plotly.

I can hover and see the individual values for each group but not the total.

like image 272
hangc Avatar asked Feb 24 '17 23:02

hangc


People also ask

How do you show figures in plotly?

To display a figure using the renderers framework, you call the . show() method on a graph object figure, or pass the figure to the plotly. io. show function.

How do you make a stacked bar graph in plotly?

Creating the Chart After importing the data, select the 'bar charts' option from 'Choose Plot Type' dropdown. This will create a raw chart as shown below. To create a stacked bar chart, from the 'Traces' popover, go to 'mode' tab, and select 'stack', as shown in figure.

What is a 100% stacked bar chart?

In a 100% stacked bar chart, the bars are split into colored bar segments placed on top of each other. Each bar height is 100%, and the colored bar segments represent the components' relative contributions to the total bar.


1 Answers

You could set the sum in the text attribute and show in the hoverinfo.

enter image description here

import plotly
plotly.offline.init_notebook_mode()

animals = ['giraffes', 'orangutans', 'monkeys']
zoo_sf = [20, 14, 23]
zoo_la = [12, 18, 29]

trace1 = plotly.graph_objs.Bar(
    x=animals,
    y=zoo_sf,
    name='SF Zoo',
    hoverinfo='text',
    text=[('Total: ' + str(x + y) + '<br>SF: ' + str(y)) for x, y in zip(zoo_la, zoo_sf)] 
)

trace2 = plotly.graph_objs.Bar(
    x=animals,
    y=zoo_la,
    name='LA Zoo',
    hoverinfo='text',
    text=[('Total: ' + str(x + y) + '<br>LA: ' + str(x)) for x, y in zip(zoo_la, zoo_sf)]
)

data = [trace1, trace2]
layout = plotly.graph_objs.Layout(barmode='stack')
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.iplot(fig)
like image 154
Maximilian Peters Avatar answered Sep 22 '22 15:09

Maximilian Peters