From 3a3b7a82b0aa60371f4e7cd2969666280afa885a Mon Sep 17 00:00:00 2001 From: SimaRaha Date: Sat, 25 Jan 2025 10:45:04 -0500 Subject: [PATCH 01/10] Update horizontal-bar-charts.md An example of a butterfly chart/diverging bar chart has been added. --- doc/python/horizontal-bar-charts.md | 78 ++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index ae9b02e3d30..430c20a5db0 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -217,6 +217,82 @@ fig.update_layout(annotations=annotations) fig.show() ``` +# Q2. Example 1: Butterfly chart/diverging bar chart-1 +import pandas as pd + +data = { + "Category": ["Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"], + "Strongly Agree": [40, 35, 50, 30, 60], + "Somewhat Agree": [30, 25, 40, 20, 49], + "Neutral": [15, 10, 20, 10, 30], + "Somewhat Disagree": [-20, -15, -25, -10, -30], + "Strongly Disagree": [-10, -50, -15, -15,-20] +} +df = pd.DataFrame(data) + +import plotly.graph_objects as go + +fig = go.Figure() + +# Add bars for each category +fig.add_trace(go.Bar( + y=df["Category"], + x=df["Strongly Agree"], + name="Strongly Agree", + orientation='h', + marker=dict(color='dark blue') +)) + +fig.add_trace(go.Bar( + y=df["Category"], + x=df["Somewhat Agree"], + name="Somewhat Agree", + orientation='h', + marker=dict(color='lightblue') +)) + +fig.add_trace(go.Bar( + y=df["Category"], + x=df["Neutral"], + name="Neutral", + orientation='h', + marker=dict(color='Lightgray') +)) + +fig.add_trace(go.Bar( + y=df["Category"], + x=df["Somewhat Disagree"], + name="Somewhat Disagree", + orientation='h', + marker=dict(color='Orange') +)) + +fig.add_trace(go.Bar( + y=df["Category"], + x=df["Strongly Disagree"], + name="Strongly Disagree", + orientation='h', + marker=dict(color='red') +)) + +fig.update_layout( + title="User Feedback on Online Education Services", + xaxis=dict( + title="Number of Responses", + zeroline=True, # Ensure there's a zero line for divergence + zerolinecolor="black", + ), + yaxis=dict(title=""), + barmode='relative', # Allows bars to diverge from the center + plot_bgcolor="white", + height=600, + width=800 +) + +fig.show() + +# Reference https://plotly.com/python/horizontal-bar-charts/#basic-horizontal-bar-chart + ### Bar Chart with Line Plot ```python @@ -335,4 +411,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 6c2426ac534eb6544d254a8d752c9e48fdf97bd1 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Sun, 26 Jan 2025 22:21:16 -0500 Subject: [PATCH 02/10] Rob's edits --- doc/python/horizontal-bar-charts.md | 90 ++++++++++++++--------------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index 430c20a5db0..caf9ff1c9b9 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -217,81 +217,77 @@ fig.update_layout(annotations=annotations) fig.show() ``` -# Q2. Example 1: Butterfly chart/diverging bar chart-1 +### Diverging Bar (or Butterfly) Chart + +Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. Implementing presentation-ready versions of them in Plotly requires a few non standard layout and legendrank options. + +``` import pandas as pd +import plotly.graph_objects as go data = { "Category": ["Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"], - "Strongly Agree": [40, 35, 50, 30, 60], "Somewhat Agree": [30, 25, 40, 20, 49], - "Neutral": [15, 10, 20, 10, 30], + "Strongly Agree": [40, 35, 50, 30, 60], "Somewhat Disagree": [-20, -15, -25, -10, -30], "Strongly Disagree": [-10, -50, -15, -15,-20] } df = pd.DataFrame(data) - -import plotly.graph_objects as go +print(df.columns) fig = go.Figure() -# Add bars for each category -fig.add_trace(go.Bar( - y=df["Category"], - x=df["Strongly Agree"], - name="Strongly Agree", - orientation='h', - marker=dict(color='dark blue') -)) +color_by_category={ + "Strongly Agree":'darkblue', + "Somewhat Agree":'lightblue', + "Somewhat Disagree":'orange', + "Strongly Disagree":'red', +} -fig.add_trace(go.Bar( - y=df["Category"], - x=df["Somewhat Agree"], - name="Somewhat Agree", - orientation='h', - marker=dict(color='lightblue') -)) +# We want the legend to be ordered in the same order that the categories appear, left to right -- +# which is different from the order in which we add the traces to the figure. +# since we need to create the "somewhat" traces first, then the "strongly" traces to display +# the segments in the desired order -fig.add_trace(go.Bar( - y=df["Category"], - x=df["Neutral"], - name="Neutral", - orientation='h', - marker=dict(color='Lightgray') -)) +legend_rank_by_category={ + "Strongly Disagree":1, + "Somewhat Disagree":2, + "Somewhat Agree":3, + "Strongly Agree":4, +} -fig.add_trace(go.Bar( - y=df["Category"], - x=df["Somewhat Disagree"], - name="Somewhat Disagree", - orientation='h', - marker=dict(color='Orange') -)) +# Add bars for each category +for col in df.columns[1:]: + fig.add_trace(go.Bar( + y=df["Category"], + x=df[col], + name=col, + orientation='h', + marker=dict(color=color_by_category[col]), + legendrank=legend_rank_by_category[col] + + )) -fig.add_trace(go.Bar( - y=df["Category"], - x=df["Strongly Disagree"], - name="Strongly Disagree", - orientation='h', - marker=dict(color='red') -)) fig.update_layout( - title="User Feedback on Online Education Services", + title="Reactions to the statement, 'The service met your expectations for':", xaxis=dict( title="Number of Responses", zeroline=True, # Ensure there's a zero line for divergence zerolinecolor="black", + # use array tick mode to show that the counts to the left of zero are still positive. + # this is hard coded; generalize this if you plan to create a function that takes unknown or widely varying data + tickmode = 'array', + tickvals = [-50, 0, 50, 100], + ticktext = [50, 0, 50, 100] ), - yaxis=dict(title=""), + yaxis_title = "", barmode='relative', # Allows bars to diverge from the center plot_bgcolor="white", - height=600, - width=800 ) fig.show() - -# Reference https://plotly.com/python/horizontal-bar-charts/#basic-horizontal-bar-chart +``` ### Bar Chart with Line Plot From aa964051522f75a57f10293b01dd6cba87a0be68 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Sun, 16 Mar 2025 15:01:05 -0400 Subject: [PATCH 03/10] switched from hard coded data to a df.read_csv from github --- doc/python/horizontal-bar-charts.md | 48 +++++++++++++---------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index caf9ff1c9b9..b86b8769cc8 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -225,39 +225,39 @@ Diverging bar charts show counts of positive outcomes or sentiments to the right import pandas as pd import plotly.graph_objects as go -data = { - "Category": ["Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"], - "Somewhat Agree": [30, 25, 40, 20, 49], - "Strongly Agree": [40, 35, 50, 30, 60], - "Somewhat Disagree": [-20, -15, -25, -10, -30], - "Strongly Disagree": [-10, -50, -15, -15,-20] -} -df = pd.DataFrame(data) -print(df.columns) -fig = go.Figure() +df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/refs/heads/master/gss_2002_5_pt_likert.csv') +#data source details are in this CSV file +df.rename(columns={'Unnamed: 0':"Category"}, inplace=True) + +#achieve the diverging effect by putting a negative sign on the "disagree" answers +for v in ["Disagree","Strongly Disagree"]: + df[v]=df[v]*-1 +fig = go.Figure() +# this color palette conveys meaning: blues for negative, reds for positive, gray for Neither Agree nor Disagree color_by_category={ "Strongly Agree":'darkblue', - "Somewhat Agree":'lightblue', - "Somewhat Disagree":'orange', + "Agree":'lightblue', + "Disagree":'orange', "Strongly Disagree":'red', + "Neither Agree nor Disagree":'gray', } -# We want the legend to be ordered in the same order that the categories appear, left to right -- -# which is different from the order in which we add the traces to the figure. -# since we need to create the "somewhat" traces first, then the "strongly" traces to display -# the segments in the desired order +# We want the legend to be ordered in the same order that the categories appear, left to right -- +# which is different from the order in which we have to add the traces to the figure. +# since we need to create the "somewhat" traces before the "strongly" traces to display +# the segments in the desired order legend_rank_by_category={ "Strongly Disagree":1, - "Somewhat Disagree":2, - "Somewhat Agree":3, + "Disagree":2, + "Agree":3, "Strongly Agree":4, + "Neither Agree nor Disagree":5 } - # Add bars for each category -for col in df.columns[1:]: +for col in ["Disagree","Strongly Disagree","Agree","Strongly Agree"]: fig.add_trace(go.Bar( y=df["Category"], x=df[col], @@ -265,14 +265,11 @@ for col in df.columns[1:]: orientation='h', marker=dict(color=color_by_category[col]), legendrank=legend_rank_by_category[col] - )) - - fig.update_layout( - title="Reactions to the statement, 'The service met your expectations for':", + title="Reactions to statements from the 2002 General Social Survey:", xaxis=dict( - title="Number of Responses", + title="Percent of Responses", zeroline=True, # Ensure there's a zero line for divergence zerolinecolor="black", # use array tick mode to show that the counts to the left of zero are still positive. @@ -285,7 +282,6 @@ fig.update_layout( barmode='relative', # Allows bars to diverge from the center plot_bgcolor="white", ) - fig.show() ``` From be0a29e6bee6bee7b31275844e1f587f8a1e4823 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Sun, 16 Mar 2025 15:08:38 -0400 Subject: [PATCH 04/10] separated update_xaxes from update_layout --- doc/python/horizontal-bar-charts.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index b86b8769cc8..69ff0751d99 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -266,9 +266,15 @@ for col in ["Disagree","Strongly Disagree","Agree","Strongly Agree"]: marker=dict(color=color_by_category[col]), legendrank=legend_rank_by_category[col] )) + fig.update_layout( title="Reactions to statements from the 2002 General Social Survey:", - xaxis=dict( + yaxis_title = "", + barmode='relative', # Allows bars to diverge from the center + plot_bgcolor="white", +) + +fig.update_xaxes( title="Percent of Responses", zeroline=True, # Ensure there's a zero line for divergence zerolinecolor="black", @@ -277,11 +283,8 @@ fig.update_layout( tickmode = 'array', tickvals = [-50, 0, 50, 100], ticktext = [50, 0, 50, 100] - ), - yaxis_title = "", - barmode='relative', # Allows bars to diverge from the center - plot_bgcolor="white", ) + fig.show() ``` From 28aa4f33f40c947140f0ed48731fa8cf398ce308 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Wed, 26 Mar 2025 13:01:21 -0400 Subject: [PATCH 05/10] removed the (irrelevant) neutral category --- doc/python/horizontal-bar-charts.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index 69ff0751d99..9f6e94b7613 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -221,8 +221,7 @@ fig.show() Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. Implementing presentation-ready versions of them in Plotly requires a few non standard layout and legendrank options. -``` -import pandas as pd +```import pandas as pd import plotly.graph_objects as go @@ -235,13 +234,12 @@ for v in ["Disagree","Strongly Disagree"]: df[v]=df[v]*-1 fig = go.Figure() -# this color palette conveys meaning: blues for negative, reds for positive, gray for Neither Agree nor Disagree +# this color palette conveys meaning: blues for positive, red and orange for negative color_by_category={ "Strongly Agree":'darkblue', "Agree":'lightblue', "Disagree":'orange', "Strongly Disagree":'red', - "Neither Agree nor Disagree":'gray', } @@ -254,7 +252,6 @@ legend_rank_by_category={ "Disagree":2, "Agree":3, "Strongly Agree":4, - "Neither Agree nor Disagree":5 } # Add bars for each category for col in ["Disagree","Strongly Disagree","Agree","Strongly Agree"]: @@ -286,6 +283,7 @@ fig.update_xaxes( ) fig.show() + ``` ### Bar Chart with Line Plot From 6b70a420a80c985d2dd2438378fdb29a7a1f3223 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Wed, 26 Mar 2025 13:01:51 -0400 Subject: [PATCH 06/10] removed unneeded comment Co-authored-by: Liam Connors --- doc/python/horizontal-bar-charts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index 9f6e94b7613..6f51f1c58cb 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -226,7 +226,7 @@ import plotly.graph_objects as go df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/refs/heads/master/gss_2002_5_pt_likert.csv') -#data source details are in this CSV file + df.rename(columns={'Unnamed: 0':"Category"}, inplace=True) #achieve the diverging effect by putting a negative sign on the "disagree" answers From ebece08ff414f8487bb8e9aeabf713fc584864a9 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Mon, 31 Mar 2025 15:15:50 -0400 Subject: [PATCH 07/10] Update doc/python/horizontal-bar-charts.md adding python decorator Co-authored-by: Liam Connors --- doc/python/horizontal-bar-charts.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index 6f51f1c58cb..ee91b997d3a 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -221,8 +221,9 @@ fig.show() Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. Implementing presentation-ready versions of them in Plotly requires a few non standard layout and legendrank options. -```import pandas as pd +```python import plotly.graph_objects as go +import pandas as pd df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/refs/heads/master/gss_2002_5_pt_likert.csv') From a67ec5420868262498ba270856059a61355a86aa Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Mon, 31 Mar 2025 15:16:09 -0400 Subject: [PATCH 08/10] removing extra sentence Co-authored-by: Liam Connors --- doc/python/horizontal-bar-charts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index ee91b997d3a..6d535824bd5 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -219,7 +219,7 @@ fig.show() ### Diverging Bar (or Butterfly) Chart -Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. Implementing presentation-ready versions of them in Plotly requires a few non standard layout and legendrank options. +Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. ```python import plotly.graph_objects as go From efc4316faba5d65fbe8e3d47f65d545b0b2178c0 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Tue, 8 Apr 2025 21:25:53 -0400 Subject: [PATCH 09/10] Moved language from #4984 per suggestion of @LiamConnors --- doc/python/horizontal-bar-charts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index 6d535824bd5..b0c26acfea7 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -219,7 +219,7 @@ fig.show() ### Diverging Bar (or Butterfly) Chart -Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. +Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. This example leaves the number of people offering a neutral response implicit because the categories add to 100%. ```python import plotly.graph_objects as go From be3b500df1197b7c12c6575009205a1bd2cc2e80 Mon Sep 17 00:00:00 2001 From: Rob Letzler <22990670+rl-utility-man@users.noreply.github.com> Date: Mon, 14 Apr 2025 10:02:35 -0400 Subject: [PATCH 10/10] Update horizontal-bar-charts.md --- doc/python/horizontal-bar-charts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/python/horizontal-bar-charts.md b/doc/python/horizontal-bar-charts.md index b0c26acfea7..35c017c9c36 100644 --- a/doc/python/horizontal-bar-charts.md +++ b/doc/python/horizontal-bar-charts.md @@ -219,7 +219,7 @@ fig.show() ### Diverging Bar (or Butterfly) Chart -Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. This example leaves the number of people offering a neutral response implicit because the categories add to 100%. +Diverging bar charts show counts of positive outcomes or sentiments to the right of zero and counts of negative outcomes to the left of zero, allowing the reader to easily spot areas of excellence and concern. This example allows the reader of the graph to infer the number of people offering a neutral response because the neutral category, which is left implicit, would make the responses add to 100%. ```python import plotly.graph_objects as go