From 7e342fe37a83602b8fc70e80ce923bf5d0c1b225 Mon Sep 17 00:00:00 2001 From: SimaRaha Date: Sat, 25 Jan 2025 10:28:54 -0500 Subject: [PATCH 1/4] Update horizontal-bar-charts.md An example of a Horizontal Bar Chart (Facet) is added. --- doc/python/horizontal-bar-charts.md | 38 ++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index ae9b02e3d30..7cd988f48ee 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -109,6 +109,42 @@ fig.add_trace(go.Bar( fig.update_layout(barmode='stack') fig.show() ``` +# 1. Example Horizontal Bar Chart (Facet) +import pandas as pd + +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) + +import plotly.express as px + +fig = px.bar( + df, + x="Outcome", + y="Region", + orientation="h", + facet_col="Quarter", + title="Quarterly Number of Patients Served by Region", + labels={"Outcome": "Patients Served", "Region": "Region"} +) + +fig.update_layout( + height=400, + title_font_size=16, + title_x=0.5, + showlegend=False, # Remove legend for simplicity + margin=dict(t=50, l=50, r=50, b=50) # Adjust margins +) + +fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1])) +# Remove duplicate y-axis labels +fig.for_each_yaxis(lambda axis: axis.update(title="Region")) +fig.for_each_xaxis(lambda axis: axis.update(title=None)) + +fig.show() ### Color Palette for Bar Chart @@ -335,4 +371,4 @@ fig.show() ### Reference -See more examples of bar charts and styling options [here](https://plotly.com/python/bar-charts/).
See https://plotly.com/python/reference/bar/ for more information and chart attribute options! \ No newline at end of file +See more examples of bar charts and styling options [here](https://plotly.com/python/bar-charts/).
See https://plotly.com/python/reference/bar/ for more information and chart attribute options! From eec9e83a7abb9cdc18c928c99cc311df07805a3e Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:40:08 -0500 Subject: [PATCH 2/4] added explanation --- doc/python/horizontal-bar-charts.md | 30 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index 7cd988f48ee..f34298bdb4f 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -109,8 +109,15 @@ fig.add_trace(go.Bar( fig.update_layout(barmode='stack') fig.show() ``` -# 1. Example Horizontal Bar Chart (Facet) +### 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, @@ -119,7 +126,6 @@ data = { } df = pd.DataFrame(data) -import plotly.express as px fig = px.bar( df, @@ -127,24 +133,28 @@ fig = px.bar( y="Region", orientation="h", facet_col="Quarter", - title="Quarterly Number of Patients Served by Region", + 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, - title_font_size=16, - title_x=0.5, - showlegend=False, # Remove legend for simplicity - margin=dict(t=50, l=50, r=50, b=50) # Adjust margins + 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 y-axis labels -fig.for_each_yaxis(lambda axis: axis.update(title="Region")) + +# 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() +``` ### Color Palette for Bar Chart From e8eb91838e682e155c572f8a3c1869c43da46de6 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:45:38 -0500 Subject: [PATCH 3/4] added a cross reference to facet plots --- doc/python/facet-plots.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/python/facet-plots.md b/doc/python/facet-plots.md index 5d0f629b2ea..ff700730017 100644 --- a/doc/python/facet-plots.md +++ b/doc/python/facet-plots.md @@ -54,6 +54,8 @@ fig.show() ### Bar Chart Row Facets +There is a similar 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() From ceb1b1799241a8d6570afb52b0d927c865992895 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Sun, 26 Jan 2025 21:46:20 -0500 Subject: [PATCH 4/4] added a cross reference to facet plots --- doc/python/facet-plots.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/facet-plots.md b/doc/python/facet-plots.md index ff700730017..7a79ea06c2d 100644 --- a/doc/python/facet-plots.md +++ b/doc/python/facet-plots.md @@ -54,7 +54,7 @@ fig.show() ### Bar Chart Row Facets -There is a similar 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) +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