Skip to content

Commit 7e342fe

Browse files
authored
Update horizontal-bar-charts.md
An example of a Horizontal Bar Chart (Facet) is added.
1 parent 861b421 commit 7e342fe

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

doc/python/horizontal-bar-charts.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,42 @@ fig.add_trace(go.Bar(
109109
fig.update_layout(barmode='stack')
110110
fig.show()
111111
```
112+
# 1. Example Horizontal Bar Chart (Facet)
113+
import pandas as pd
114+
115+
data = {
116+
"Quarter": ["Q1", "Q2", "Q3", "Q4"] * 3,
117+
"Region": ["North", "North", "North", "North", "South", "South", "South", "South", "West", "West", "West", "West"],
118+
"Outcome": [150, 200, 250, 300, 120, 180, 240, 310, 100, 150, 220, 280]
119+
}
120+
df = pd.DataFrame(data)
121+
122+
import plotly.express as px
123+
124+
fig = px.bar(
125+
df,
126+
x="Outcome",
127+
y="Region",
128+
orientation="h",
129+
facet_col="Quarter",
130+
title="Quarterly Number of Patients Served by Region",
131+
labels={"Outcome": "Patients Served", "Region": "Region"}
132+
)
133+
134+
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
140+
)
141+
142+
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"))
145+
fig.for_each_xaxis(lambda axis: axis.update(title=None))
146+
147+
fig.show()
112148

113149
### Color Palette for Bar Chart
114150

@@ -335,4 +371,4 @@ fig.show()
335371

336372
### Reference
337373

338-
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!
374+
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!

0 commit comments

Comments
 (0)