@@ -225,54 +225,51 @@ Diverging bar charts show counts of positive outcomes or sentiments to the right
225
225
import pandas as pd
226
226
import plotly.graph_objects as go
227
227
228
- data = {
229
- "Category": ["Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"],
230
- "Somewhat Agree": [30, 25, 40, 20, 49],
231
- "Strongly Agree": [40, 35, 50, 30, 60],
232
- "Somewhat Disagree": [-20, -15, -25, -10, -30],
233
- "Strongly Disagree": [-10, -50, -15, -15,-20]
234
- }
235
- df = pd.DataFrame(data)
236
- print(df.columns)
237
228
238
- fig = go.Figure()
229
+ df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/refs/heads/master/gss_2002_5_pt_likert.csv')
230
+ #data source details are in this CSV file
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
239
236
237
+ fig = go.Figure()
238
+ # this color palette conveys meaning: blues for negative, reds for positive, gray for Neither Agree nor Disagree
240
239
color_by_category={
241
240
"Strongly Agree":'darkblue',
242
- "Somewhat Agree":'lightblue',
243
- "Somewhat Disagree":'orange',
241
+ "Agree":'lightblue',
242
+ "Disagree":'orange',
244
243
"Strongly Disagree":'red',
244
+ "Neither Agree nor Disagree":'gray',
245
245
}
246
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 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
251
247
248
+ # We want the legend to be ordered in the same order that the categories appear, left to right --
249
+ # which is different from the order in which we have to add the traces to the figure.
250
+ # since we need to create the "somewhat" traces before the "strongly" traces to display
251
+ # the segments in the desired order
252
252
legend_rank_by_category={
253
253
"Strongly Disagree":1,
254
- "Somewhat Disagree":2,
255
- "Somewhat Agree":3,
254
+ "Disagree":2,
255
+ "Agree":3,
256
256
"Strongly Agree":4,
257
+ "Neither Agree nor Disagree":5
257
258
}
258
-
259
259
# Add bars for each category
260
- for col in df.columns[1: ]:
260
+ for col in ["Disagree","Strongly Disagree","Agree","Strongly Agree" ]:
261
261
fig.add_trace(go.Bar(
262
262
y=df["Category"],
263
263
x=df[col],
264
264
name=col,
265
265
orientation='h',
266
266
marker=dict(color=color_by_category[col]),
267
267
legendrank=legend_rank_by_category[col]
268
-
269
268
))
270
-
271
-
272
269
fig.update_layout(
273
- title="Reactions to the statement, 'The service met your expectations for' :",
270
+ title="Reactions to statements from the 2002 General Social Survey :",
274
271
xaxis=dict(
275
- title="Number of Responses",
272
+ title="Percent of Responses",
276
273
zeroline=True, # Ensure there's a zero line for divergence
277
274
zerolinecolor="black",
278
275
# use array tick mode to show that the counts to the left of zero are still positive.
@@ -285,7 +282,6 @@ fig.update_layout(
285
282
barmode='relative', # Allows bars to diverge from the center
286
283
plot_bgcolor="white",
287
284
)
288
-
289
285
fig.show()
290
286
```
291
287
0 commit comments