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
#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
236
237
237
fig = go.Figure()
238
-
# this color palette conveys meaning: blues for negative, reds for positive, gray for neutral
238
+
# this color palette conveys meaning: blues for negative, reds for positive, gray for Neither Agree nor Disagree
239
239
color_by_category={
240
240
"Strongly Agree":'darkblue',
241
-
"Somewhat Agree":'lightblue',
242
-
"Somewhat Disagree":'orange',
241
+
"Agree":'lightblue',
242
+
"Disagree":'orange',
243
243
"Strongly Disagree":'red',
244
-
"Neutral":'gray',
244
+
"Neither Agree nor Disagree":'gray',
245
245
}
246
246
247
+
247
248
# We want the legend to be ordered in the same order that the categories appear, left to right --
248
249
# which is different from the order in which we have to add the traces to the figure.
249
250
# since we need to create the "somewhat" traces before the "strongly" traces to display
250
251
# the segments in the desired order
251
252
252
253
legend_rank_by_category={
253
254
"Strongly Disagree":1,
254
-
"Somewhat Disagree":2,
255
-
"Somewhat Agree":3,
255
+
"Disagree":2,
256
+
"Agree":3,
256
257
"Strongly Agree":4,
257
-
"Neutral":5
258
+
"Neither Agree nor Disagree":5
258
259
}
259
260
260
261
# Add bars
261
-
for col in df[["Somewhat Disagree","Strongly Disagree","Somewhat Agree","Strongly Agree","Neutral"]]:
262
+
for col in df[["Disagree","Strongly Disagree","Agree","Strongly Agree","Neither Agree nor Disagree"]]:
262
263
fig.add_trace(go.Bar(
263
264
y=df["Category"],
264
265
x=df[col],
265
266
name=col,
266
267
orientation='h',
267
268
marker=dict(color=color_by_category[col]),
268
269
legendrank=legend_rank_by_category[col],
269
-
xaxis=f"x{1+(col=="Neutral")}", # in this context, putting neutral on a secondary x-axis on a different ___domain
270
+
xaxis=f"x{1+(col=="Neither Agree nor Disagree")}", # in this context, putting "Neither Agree nor Disagree" on a secondary x-axis on a different ___domain
270
271
# yields results equivalent to subplots with far less code
271
-
272
-
273
272
)
274
273
)
275
274
@@ -278,15 +277,16 @@ for col in df[["Somewhat Disagree","Strongly Disagree","Somewhat Agree","Strongl
278
277
279
278
# Find the maximum width of the bars to the left and right sides of the origin; remember that the width of
280
279
# the plot is the sum of the longest negative bar and the longest positive bar even if they are on separate rows
0 commit comments