@@ -225,54 +225,51 @@ Diverging bar charts show counts of positive outcomes or sentiments to the right
225225import pandas as pd
226226import plotly.graph_objects as go
227227
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)
237228
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
239236
237+ fig = go.Figure()
238+ # this color palette conveys meaning: blues for negative, reds for positive, gray for Neither Agree nor Disagree
240239color_by_category={
241240 "Strongly Agree":'darkblue',
242- "Somewhat Agree":'lightblue',
243- "Somewhat Disagree":'orange',
241+ "Agree":'lightblue',
242+ "Disagree":'orange',
244243 "Strongly Disagree":'red',
244+ "Neither Agree nor Disagree":'gray',
245245}
246246
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
251247
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
252252legend_rank_by_category={
253253 "Strongly Disagree":1,
254- "Somewhat Disagree":2,
255- "Somewhat Agree":3,
254+ "Disagree":2,
255+ "Agree":3,
256256 "Strongly Agree":4,
257+ "Neither Agree nor Disagree":5
257258}
258-
259259# Add bars for each category
260- for col in df.columns[1: ]:
260+ for col in ["Disagree","Strongly Disagree","Agree","Strongly Agree" ]:
261261 fig.add_trace(go.Bar(
262262 y=df["Category"],
263263 x=df[col],
264264 name=col,
265265 orientation='h',
266266 marker=dict(color=color_by_category[col]),
267267 legendrank=legend_rank_by_category[col]
268-
269268 ))
270-
271-
272269fig.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 :",
274271 xaxis=dict(
275- title="Number of Responses",
272+ title="Percent of Responses",
276273 zeroline=True, # Ensure there's a zero line for divergence
277274 zerolinecolor="black",
278275 # use array tick mode to show that the counts to the left of zero are still positive.
@@ -285,7 +282,6 @@ fig.update_layout(
285282 barmode='relative', # Allows bars to diverge from the center
286283 plot_bgcolor="white",
287284)
288-
289285fig.show()
290286```
291287
0 commit comments