-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
adding a diverging bar example to the horizontal bar documentation #4994
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3a3b7a8
Update horizontal-bar-charts.md
SimaRaha 6c2426a
Rob's edits
rl-utility-man 0bac79d
Merge branch 'master' into patch-2
rl-utility-man aa96405
switched from hard coded data to a df.read_csv from github
rl-utility-man be0a29e
separated update_xaxes from update_layout
rl-utility-man 28aa4f3
removed the (irrelevant) neutral category
rl-utility-man 6b70a42
removed unneeded comment
rl-utility-man ebece08
Update doc/python/horizontal-bar-charts.md
rl-utility-man a67ec54
removing extra sentence
rl-utility-man de7f742
Merge branch 'main' into patch-2
LiamConnors efc4316
Moved language from #4984 per suggestion of @LiamConnors
rl-utility-man be3b500
Update horizontal-bar-charts.md
rl-utility-man File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,77 @@ fig.update_layout(annotations=annotations) | |
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. | ||
|
||
``` | ||
import pandas as pd | ||
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) | ||
rl-utility-man marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#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', | ||
"Agree":'lightblue', | ||
"Disagree":'orange', | ||
"Strongly Disagree":'red', | ||
"Neither Agree nor Disagree":'gray', | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this say "blues for positive" and "reds for negative"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, corrected. |
||
|
||
|
||
# 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, | ||
"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"]: | ||
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] | ||
)) | ||
LiamConnors marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fig.update_layout( | ||
title="Reactions to statements from the 2002 General Social Survey:", | ||
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", | ||
# 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] | ||
) | ||
|
||
fig.show() | ||
``` | ||
|
||
### Bar Chart with Line Plot | ||
|
||
```python | ||
|
@@ -335,4 +406,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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.