Skip to content

Commit eec9e83

Browse files
added explanation
1 parent 7e342fe commit eec9e83

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

doc/python/horizontal-bar-charts.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,15 @@ fig.add_trace(go.Bar(
109109
fig.update_layout(barmode='stack')
110110
fig.show()
111111
```
112-
# 1. Example Horizontal Bar Chart (Facet)
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+
.
117+
118+
```
113119
import pandas as pd
120+
import plotly.express as px
114121
115122
data = {
116123
"Quarter": ["Q1", "Q2", "Q3", "Q4"] * 3,
@@ -119,32 +126,35 @@ data = {
119126
}
120127
df = pd.DataFrame(data)
121128
122-
import plotly.express as px
123129
124130
fig = px.bar(
125131
df,
126132
x="Outcome",
127133
y="Region",
128134
orientation="h",
129135
facet_col="Quarter",
130-
title="Quarterly Number of Patients Served by Region",
136+
title="Number of Patients Served by Region and Quarter",
131137
labels={"Outcome": "Patients Served", "Region": "Region"}
132138
)
133139
140+
## the section below is optional clean up to make this presentation ready
141+
134142
fig.update_layout(
135-
height=400,
136-
title_font_size=16,
137-
title_x=0.5,
138-
showlegend=False, # Remove legend for simplicity
139-
margin=dict(t=50, l=50, r=50, b=50) # Adjust margins
143+
height=400, #the Plotly default makes the bars awkwardly large; setting a height improves the display
144+
showlegend=False, # the legend does not add anything
140145
)
141146
147+
#remove up the default "facet_variable =" text from the title of each facet graph
142148
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
143-
# Remove duplicate y-axis labels
144-
fig.for_each_yaxis(lambda axis: axis.update(title="Region"))
149+
150+
# Remove duplicate axis labels
151+
fig.for_each_yaxis(lambda axis: axis.update(title=None))
145152
fig.for_each_xaxis(lambda axis: axis.update(title=None))
153+
# add the one valuable axis label back in
154+
fig.update_xaxes(title="Count", row=1, col=1)
146155
147156
fig.show()
157+
```
148158

149159
### Color Palette for Bar Chart
150160

0 commit comments

Comments
 (0)