Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a horizontal bar small multiples example #4995

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/python/facet-plots.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ fig.show()

### Bar Chart Row Facets

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)

```python
import plotly.express as px
df = px.data.tips()
Expand Down
48 changes: 47 additions & 1 deletion doc/python/horizontal-bar-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,52 @@ fig.add_trace(go.Bar(
))

fig.update_layout(barmode='stack')
fig.show()
```
### Small multiple horizontal bar charts show each component's size more clearly than a stacked bar

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.

.

```
import pandas as pd
import plotly.express as px

data = {
"Quarter": ["Q1", "Q2", "Q3", "Q4"] * 3,
"Region": ["North", "North", "North", "North", "South", "South", "South", "South", "West", "West", "West", "West"],
"Outcome": [150, 200, 250, 300, 120, 180, 240, 310, 100, 150, 220, 280]
}
df = pd.DataFrame(data)


fig = px.bar(
df,
x="Outcome",
y="Region",
orientation="h",
facet_col="Quarter",
title="Number of Patients Served by Region and Quarter",
labels={"Outcome": "Patients Served", "Region": "Region"}
)

## the section below is optional clean up to make this presentation ready

fig.update_layout(
height=400, #the Plotly default makes the bars awkwardly large; setting a height improves the display
showlegend=False, # the legend does not add anything
)

#remove up the default "facet_variable =" text from the title of each facet graph
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

# Remove duplicate axis labels
fig.for_each_yaxis(lambda axis: axis.update(title=None))
fig.for_each_xaxis(lambda axis: axis.update(title=None))
# add the one valuable axis label back in
fig.update_xaxes(title="Count", row=1, col=1)

fig.show()
```

Expand Down Expand Up @@ -335,4 +381,4 @@ fig.show()

### Reference

See more examples of bar charts and styling options [here](https://plotly.com/python/bar-charts/).<br> See https://plotly.com/python/reference/bar/ for more information and chart attribute options!
See more examples of bar charts and styling options [here](https://plotly.com/python/bar-charts/).<br> See https://plotly.com/python/reference/bar/ for more information and chart attribute options!