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
improved the comments.
  • Loading branch information
rl-utility-man authored Mar 26, 2025
commit 70b7f49b0036b0873c88a08fcd11f1f493caeb7c
11 changes: 6 additions & 5 deletions doc/python/horizontal-bar-charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,14 @@ fig.show()
```
### Diverging Bar (or Butterfly) Chart with Neutral Column

Diverging bar charts offer two imperfect options for responses that are neither positive nor negative: omit them, leaving them implicit when the categories add to 100%, as we did above or put them in a separate column, as we do in this example. Jonathan Schwabish discusses this on page 92-97 of _Better Data Visualizations_.
Diverging bar charts offer two imperfect options for responses that are neither positive nor negative: put them in a separate column, as in this example or omit them as in the example above. That leaves the unreported neutral value implicit when the categories add to 100%, Jonathan Schwabish discusses this on page 92-97 of _Better Data Visualizations_.

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


Expand All @@ -235,7 +234,7 @@ 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 agreement, reds and oranges for disagreement, gray for Neither Agree nor Disagree
color_by_category={
"Strongly Agree":'darkblue',
"Agree":'lightblue',
Expand Down Expand Up @@ -285,25 +284,27 @@ max_width_signed = abs(max_left)+max_right
max_width_neither = max(df["Neither Agree nor Disagree"])

fig.update_layout(

title="Reactions to statements from the 2002 General Social Survey:",
plot_bgcolor="white",
barmode='relative', # Allows bars to diverge from the center
)

fig.update_xaxes(
zeroline=True, #the zero line distinguishes between positive and negative segments
zerolinecolor="black",
#starting here, we set domain and range to create a shared x-axis scale
# multiply by .98 to add space between the two columns
range=[max_left, max_right],
domain=[0, 0.98*(max_width_signed/(max_width_signed+max_width_neither))]
)
)

fig.update_layout(
xaxis2=dict(
range=[0, max_width_neither],
domain=[(1-.98*(1-max_width_signed/(max_width_signed+max_width_neither))), 1.0],
)
)

fig.update_legends(
orientation="h", # a horizontal legend matches the horizontal bars
yref="container",
Expand Down