Skip to content

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 12 commits into from
Apr 17, 2025
72 changes: 71 additions & 1 deletion doc/python/horizontal-bar-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,76 @@ 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. This example leaves the number of people offering a neutral response implicit because the categories add to 100%.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and it's ready to merge, but I just want to check the wording on "This example leaves the number of people offering a neutral response implicit because the categories add to 100%." Should this say "
This example leaves the number of people offering a neutral response implicit because the categories don't add up to 100%."
Is that how we know there are neutral responses that are not shown here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Good point. I tried a rewrite of your wording that's hopefully a little more direct. I'm open to your wording as well.

```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')

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 positive, red and orange for negative
color_by_category={
"Strongly Agree":'darkblue',
"Agree":'lightblue',
"Disagree":'orange',
"Strongly Disagree":'red',
}


# 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,
}
# 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]
))

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
Expand Down Expand Up @@ -335,4 +405,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!