Skip to content

Example 2 for Butterfly chart (version2) #4984

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

Open
wants to merge 13 commits into
base: doc-prod
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
attempted to manually merge the latest changes to doc-prod so the new…
… contribution is the second butterfly example
  • Loading branch information
rl-utility-man committed Apr 30, 2025
commit 8d0a2eb5d8e3e7cc3b2a748be71b906299eef071
82 changes: 77 additions & 5 deletions doc/python/horizontal-bar-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ jupyter:
text_representation:
extension: .md
format_name: markdown
format_version: '1.1'
jupytext_version: 1.1.1
format_version: '1.3'
jupytext_version: 1.16.4
kernelspec:
display_name: Python 3
display_name: Python 3 (ipykernel)
language: python
name: python3
language_info:
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.7
version: 3.11.10
plotly:
description: How to make horizontal bar charts in Python with Plotly.
display_as: basic
Expand Down Expand Up @@ -216,6 +216,78 @@ 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 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
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()

```


### Diverging Bar (or Butterfly) Chart with Neutral Column

The previous diverging bar chart example excluded neutral responses. This variation includes them in a separate column. Jonathan Schwabish discusses tradeoffs between these options on page 92-97 of _Better Data Visualizations_.
Expand Down Expand Up @@ -439,4 +511,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!