Skip to content

Commit dd42534

Browse files
committed
add grouped stacked bar chart example
1 parent 593a1ed commit dd42534

File tree

1 file changed

+79
-21
lines changed

1 file changed

+79
-21
lines changed

doc/python/bar-charts.md

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.3'
9-
jupytext_version: 1.16.1
9+
jupytext_version: 1.16.4
1010
kernelspec:
1111
display_name: Python 3 (ipykernel)
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.10.11
23+
version: 3.11.10
2424
plotly:
2525
description: How to make Bar Charts in Python with Plotly.
2626
display_as: basic
@@ -304,6 +304,83 @@ fig.update_layout(barmode='stack')
304304
fig.show()
305305
```
306306

307+
### Bar Chart with Relative Barmode
308+
309+
With "relative" barmode, the bars are stacked on top of one another, with negative values
310+
below the axis, positive values above.
311+
312+
```python
313+
import plotly.graph_objects as go
314+
x = [1, 2, 3, 4]
315+
316+
fig = go.Figure()
317+
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16]))
318+
fig.add_trace(go.Bar(x=x, y=[6, -8, -4.5, 8]))
319+
fig.add_trace(go.Bar(x=x, y=[-15, -3, 4.5, -8]))
320+
fig.add_trace(go.Bar(x=x, y=[-1, 3, -3, -4]))
321+
322+
fig.update_layout(barmode='relative', title_text='Relative Barmode')
323+
fig.show()
324+
```
325+
326+
### Grouped Stacked Bar Chart
327+
328+
*Supported in Plotly.py 6.0.0 and later*
329+
330+
Use the `offsetgroup` property with `barmode="stacked"` or `barmode="relative"` to create grouped stacked bar charts. Bars that have the same `offsetgroup` will share the same position on the axis. Bars with no `offsetgroup` set will also share the same position on the axis. In the following example, for each quarter, the value for cities that belong to the same `offsetgroup` are stacked together.
331+
332+
```python
333+
import plotly.graph_objects as go
334+
335+
data = [
336+
go.Bar(
337+
x=['Q1', 'Q2', 'Q3', 'Q4'],
338+
y=[150, 200, 250, 300],
339+
name='New York',
340+
offsetgroup="USA"
341+
),
342+
go.Bar(
343+
x=['Q1', 'Q2', 'Q3', 'Q4'],
344+
y=[180, 220, 270, 320],
345+
name='Boston',
346+
offsetgroup="USA"
347+
),
348+
go.Bar(
349+
x=['Q1', 'Q2', 'Q3', 'Q4'],
350+
y=[130, 170, 210, 260],
351+
name='Montreal',
352+
offsetgroup="Canada"
353+
),
354+
go.Bar(
355+
x=['Q1', 'Q2', 'Q3', 'Q4'],
356+
y=[160, 210, 260, 310],
357+
name='Toronto',
358+
offsetgroup="Canada"
359+
)
360+
]
361+
362+
layout = go.Layout(
363+
title={
364+
'text': 'Quarterly Sales by City, Grouped by Country'
365+
},
366+
xaxis={
367+
'title': {
368+
'text': 'Quarter'
369+
}
370+
},
371+
yaxis={
372+
'title': {
373+
'text': 'Sales'
374+
}
375+
},
376+
barmode='stack'
377+
)
378+
379+
fig = go.Figure(data=data, layout=layout)
380+
381+
fig.show()
382+
```
383+
307384
### Stacked Bar Chart From Aggregating a DataFrame
308385

309386
Stacked bar charts are a powerful way to present results summarizing categories generated using the Pandas aggregate commands. `pandas.DataFrame.agg` produces a wide data set format incompatible with `px.bar`. Transposing and updating the indexes to achieve `px.bar` compatibility is a somewhat involved option. Here is one straightforward alternative, which presents the aggregated data as a stacked bar using plotly.graph_objects.
@@ -639,25 +716,6 @@ fig.update_layout(
639716
fig.show()
640717
```
641718

642-
### Bar Chart with Relative Barmode
643-
644-
With "relative" barmode, the bars are stacked on top of one another, with negative values
645-
below the axis, positive values above.
646-
647-
```python
648-
import plotly.graph_objects as go
649-
x = [1, 2, 3, 4]
650-
651-
fig = go.Figure()
652-
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 16]))
653-
fig.add_trace(go.Bar(x=x, y=[6, -8, -4.5, 8]))
654-
fig.add_trace(go.Bar(x=x, y=[-15, -3, 4.5, -8]))
655-
fig.add_trace(go.Bar(x=x, y=[-1, 3, -3, -4]))
656-
657-
fig.update_layout(barmode='relative', title_text='Relative Barmode')
658-
fig.show()
659-
```
660-
661719
### Bar Chart with Sorted or Ordered Categories
662720

663721
Set `categoryorder` to `"category ascending"` or `"category descending"` for the alphanumerical order of the category names or `"total ascending"` or `"total descending"` for numerical order of values. [categoryorder](https://plotly.com/python/reference/layout/xaxis/#layout-xaxis-categoryorder) for more information. Note that sorting the bars by a particular trace isn't possible right now - it's only possible to sort by the total values. Of course, you can always sort your data _before_ plotting it if you need more customization.

0 commit comments

Comments
 (0)