You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Q2. Example 1: Butterfly chart/diverging bar chart-1
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. Implementing presentation-ready versions of them in Plotly requires a few non standard layout and legendrank options.
223
+
224
+
```
221
225
import pandas as pd
226
+
import plotly.graph_objects as go
222
227
223
228
data = {
224
229
"Category": ["Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"],
225
-
"Strongly Agree": [40, 35, 50, 30, 60],
226
230
"Somewhat Agree": [30, 25, 40, 20, 49],
227
-
"Neutral": [15, 10, 20, 10, 30],
231
+
"Strongly Agree": [40, 35, 50, 30, 60],
228
232
"Somewhat Disagree": [-20, -15, -25, -10, -30],
229
233
"Strongly Disagree": [-10, -50, -15, -15,-20]
230
234
}
231
235
df = pd.DataFrame(data)
232
-
233
-
import plotly.graph_objects as go
236
+
print(df.columns)
234
237
235
238
fig = go.Figure()
236
239
237
-
# Add bars for each category
238
-
fig.add_trace(go.Bar(
239
-
y=df["Category"],
240
-
x=df["Strongly Agree"],
241
-
name="Strongly Agree",
242
-
orientation='h',
243
-
marker=dict(color='dark blue')
244
-
))
240
+
color_by_category={
241
+
"Strongly Agree":'darkblue',
242
+
"Somewhat Agree":'lightblue',
243
+
"Somewhat Disagree":'orange',
244
+
"Strongly Disagree":'red',
245
+
}
245
246
246
-
fig.add_trace(go.Bar(
247
-
y=df["Category"],
248
-
x=df["Somewhat Agree"],
249
-
name="Somewhat Agree",
250
-
orientation='h',
251
-
marker=dict(color='lightblue')
252
-
))
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 add the traces to the figure.
249
+
# since we need to create the "somewhat" traces first, then the "strongly" traces to display
250
+
# the segments in the desired order
253
251
254
-
fig.add_trace(go.Bar(
255
-
y=df["Category"],
256
-
x=df["Neutral"],
257
-
name="Neutral",
258
-
orientation='h',
259
-
marker=dict(color='Lightgray')
260
-
))
252
+
legend_rank_by_category={
253
+
"Strongly Disagree":1,
254
+
"Somewhat Disagree":2,
255
+
"Somewhat Agree":3,
256
+
"Strongly Agree":4,
257
+
}
261
258
262
-
fig.add_trace(go.Bar(
263
-
y=df["Category"],
264
-
x=df["Somewhat Disagree"],
265
-
name="Somewhat Disagree",
266
-
orientation='h',
267
-
marker=dict(color='Orange')
268
-
))
259
+
# Add bars for each category
260
+
for col in df.columns[1:]:
261
+
fig.add_trace(go.Bar(
262
+
y=df["Category"],
263
+
x=df[col],
264
+
name=col,
265
+
orientation='h',
266
+
marker=dict(color=color_by_category[col]),
267
+
legendrank=legend_rank_by_category[col]
268
+
269
+
))
269
270
270
-
fig.add_trace(go.Bar(
271
-
y=df["Category"],
272
-
x=df["Strongly Disagree"],
273
-
name="Strongly Disagree",
274
-
orientation='h',
275
-
marker=dict(color='red')
276
-
))
277
271
278
272
fig.update_layout(
279
-
title="User Feedback on Online Education Services",
273
+
title="Reactions to the statement, 'The service met your expectations for':",
280
274
xaxis=dict(
281
275
title="Number of Responses",
282
276
zeroline=True, # Ensure there's a zero line for divergence
283
277
zerolinecolor="black",
278
+
# use array tick mode to show that the counts to the left of zero are still positive.
279
+
# this is hard coded; generalize this if you plan to create a function that takes unknown or widely varying data
280
+
tickmode = 'array',
281
+
tickvals = [-50, 0, 50, 100],
282
+
ticktext = [50, 0, 50, 100]
284
283
),
285
-
yaxis=dict(title=""),
284
+
yaxis_title = "",
286
285
barmode='relative', # Allows bars to diverge from the center
0 commit comments