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.
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.
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.
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.
You could set the sum in the text
attribute and show in the hoverinfo
.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With