Skip to content

Commit fdf94de

Browse files
authored
Example 2 for Butterfly chart (version2)
1 parent 861b421 commit fdf94de

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

doc/python/horizontal-bar-charts.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,88 @@ fig.update_layout(annotations=annotations)
216216

217217
fig.show()
218218
```
219+
# *Example2 for Butterfly chart version 2
220+
221+
import pandas as pd
222+
import plotly.graph_objects as go
223+
224+
data = {
225+
"State": ["California", "Texas", "Florida", "New York", "Illinois", "Pennsylvania", "Ohio", "Georgia", "North Carolina", "Michigan"],
226+
"Strongly Disagree": [-12, -15, -10, -8, -7, -9, -10, -6, -7, -8],
227+
"Disagree": [-22, -25, -18, -20, -19, -21, -18, -16, -17, -15],
228+
"Agree": [32, 30, 35, 33, 36, 34, 38, 40, 37, 38],
229+
"Strongly Agree": [28, 30, 33, 31, 34, 36, 35, 38, 36, 37],
230+
"Neutral": [18, 20, 22, 19, 20, 21, 25, 23, 22, 24]
231+
}
232+
233+
df = pd.DataFrame(data)
234+
235+
fig = go.Figure()
236+
237+
# Define diverging categories and their colors
238+
categories = ["Strongly Disagree", "Disagree", "Agree", "Strongly Agree"]
239+
color_map = {
240+
"Strongly Disagree": "darkblue",
241+
"Disagree": "lightblue",
242+
"Agree": "orange",
243+
"Strongly Agree": "red",
244+
245+
}
246+
247+
# Add diverging bars for each category
248+
for category in categories:
249+
fig.add_trace(go.Bar(
250+
y=df["State"],
251+
x=df[category],
252+
name=category,
253+
orientation="h",
254+
marker=dict(color=color_map[category])
255+
))
256+
257+
258+
fig.add_trace(go.Bar(
259+
y=df["State"],
260+
x=df["Neutral"],
261+
name="Neutral",
262+
orientation="h",
263+
marker=dict(color="gray"),
264+
xaxis="x2" # Assign Neutral to a second x-axis
265+
))
266+
267+
# Update layout
268+
fig.update_layout(
269+
title="It is the responsibility of government to reduce income differences (U.S. States)",
270+
xaxis=dict(
271+
title="Percentage of Responses (Diverging Categories)",
272+
zeroline=True,
273+
zerolinecolor="black",
274+
range=[-50, 50],
275+
domain=[0, 0.8]
276+
),
277+
xaxis2=dict(
278+
title="Neutral Responses (%)",
279+
range=[0, 30],
280+
domain=[0.85, 1.0]
281+
),
282+
yaxis=dict(
283+
title="State",
284+
autorange="reversed"
285+
),
286+
barmode="relative",
287+
plot_bgcolor="white",
288+
height=600,
289+
width=1000,
290+
legend=dict(
291+
orientation="h",
292+
yanchor="bottom",
293+
y=1.02,
294+
xanchor="center",
295+
x=0.5
296+
)
297+
)
298+
299+
300+
fig.show()
219301

220302
### Bar Chart with Line Plot
221303

@@ -335,4 +417,4 @@ fig.show()
335417

336418
### Reference
337419

338-
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!
420+
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!

0 commit comments

Comments
 (0)