@@ -217,6 +217,77 @@ fig.update_layout(annotations=annotations)
217
217
fig.show()
218
218
```
219
219
220
+ ### Diverging Bar (or Butterfly) Chart
221
+
222
+ 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%.
223
+
224
+ ``` python
225
+ import plotly.graph_objects as go
226
+ import pandas as pd
227
+
228
+
229
+ df = pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/refs/heads/master/gss_2002_5_pt_likert.csv' )
230
+
231
+ df.rename(columns = {' Unnamed: 0' :" Category" }, inplace = True )
232
+
233
+ # achieve the diverging effect by putting a negative sign on the "disagree" answers
234
+ for v in [" Disagree" ," Strongly Disagree" ]:
235
+ df[v]= df[v]*- 1
236
+
237
+ fig = go.Figure()
238
+ # this color palette conveys meaning: blues for positive, red and orange for negative
239
+ color_by_category= {
240
+ " Strongly Agree" :' darkblue' ,
241
+ " Agree" :' lightblue' ,
242
+ " Disagree" :' orange' ,
243
+ " Strongly Disagree" :' red' ,
244
+ }
245
+
246
+
247
+ # We want the legend to be ordered in the same order that the categories appear, left to right --
248
+ # which is different from the order in which we have to add the traces to the figure.
249
+ # since we need to create the "somewhat" traces before the "strongly" traces to display
250
+ # the segments in the desired order
251
+ legend_rank_by_category= {
252
+ " Strongly Disagree" :1 ,
253
+ " Disagree" :2 ,
254
+ " Agree" :3 ,
255
+ " Strongly Agree" :4 ,
256
+ }
257
+ # Add bars for each category
258
+ for col in [" Disagree" ," Strongly Disagree" ," Agree" ," Strongly Agree" ]:
259
+ fig.add_trace(go.Bar(
260
+ y = df[" Category" ],
261
+ x = df[col],
262
+ name = col,
263
+ orientation = ' h' ,
264
+ marker = dict (color = color_by_category[col]),
265
+ legendrank = legend_rank_by_category[col]
266
+ ))
267
+
268
+ fig.update_layout(
269
+ title = " Reactions to statements from the 2002 General Social Survey:" ,
270
+ yaxis_title = " " ,
271
+ barmode = ' relative' , # Allows bars to diverge from the center
272
+ plot_bgcolor = " white" ,
273
+ )
274
+
275
+ fig.update_xaxes(
276
+ title = " Percent of Responses" ,
277
+ zeroline = True , # Ensure there's a zero line for divergence
278
+ zerolinecolor = " black" ,
279
+ # use array tick mode to show that the counts to the left of zero are still positive.
280
+ # this is hard coded; generalize this if you plan to create a function that takes unknown or widely varying data
281
+ tickmode = ' array' ,
282
+ tickvals = [- 50 , 0 , 50 , 100 ],
283
+ ticktext = [50 , 0 , 50 , 100 ]
284
+ )
285
+
286
+ fig.show()
287
+
288
+ ```
289
+
290
+
220
291
### Bar Chart with Line Plot
221
292
222
293
``` python
0 commit comments