Skip to content

Commit 82535ce

Browse files
authored
Merge pull request #4995 from SimaRaha/patch-1
Adding a horizontal bar small multiples example
2 parents 09c99d3 + 174e9c5 commit 82535ce

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

doc/python/facet-plots.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fig.show()
5454

5555
### Bar Chart Row Facets
5656

57+
There is a more presentation-ready horizontal, faceted bar chart in the [horizontal bar documentation](/python/horizontal-bar-charts/#Small-multiple-horizontal-bar-charts-show-each-component's-size-more-clearly-than-a-stacked-bar)
58+
5759
```python
5860
import plotly.express as px
5961
df = px.data.tips()

doc/python/horizontal-bar-charts.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,49 @@ fig.add_trace(go.Bar(
107107
))
108108

109109
fig.update_layout(barmode='stack')
110+
fig.show()
111+
```
112+
### Small multiple horizontal bar charts show each component's size more clearly than a stacked bar
113+
114+
Bar charts with multiple components pose a fundamental trade off between presenting the total clearly and presenting the component values clearly. This small multiples approach shows the component magnitudes clearly at the cost of slightly obscuring the totals. A stacked bar does the opposite. Small multiple bar charts often work better in a horizontal orientation; and are easy to create with the px.bar orientation and facet_col parameters.
115+
```
116+
import pandas as pd
117+
import plotly.express as px
118+
119+
data = {
120+
"Quarter": ["Q1", "Q2", "Q3", "Q4"] * 3,
121+
"Region": ["North", "North", "North", "North", "South", "South", "South", "South", "West", "West", "West", "West"],
122+
"Outcome": [150, 200, 250, 300, 120, 180, 240, 310, 100, 150, 220, 280]
123+
}
124+
df = pd.DataFrame(data)
125+
126+
127+
fig = px.bar(
128+
df,
129+
x="Outcome",
130+
y="Region",
131+
orientation="h",
132+
facet_col="Quarter",
133+
title="Number of Patients Served by Region and Quarter",
134+
labels={"Outcome": "Patients Served", "Region": "Region"}
135+
)
136+
137+
## the section below is optional clean up to make this presentation ready
138+
139+
fig.update_layout(
140+
height=400, #the Plotly default makes the bars awkwardly large; setting a height improves the display
141+
showlegend=False, # the legend does not add anything
142+
)
143+
144+
# remove the default "facet_variable =" text from the title of each facet graph
145+
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
146+
147+
# Remove duplicate axis labels
148+
fig.for_each_yaxis(lambda axis: axis.update(title=None))
149+
fig.for_each_xaxis(lambda axis: axis.update(title=None))
150+
# add the one valuable axis label back in
151+
fig.update_xaxes(title="Count", row=1, col=1)
152+
110153
fig.show()
111154
```
112155

0 commit comments

Comments
 (0)