Skip to content

Commit 6c2426a

Browse files
Rob's edits
1 parent 3a3b7a8 commit 6c2426a

File tree

1 file changed

+43
-47
lines changed

1 file changed

+43
-47
lines changed

doc/python/horizontal-bar-charts.md

+43-47
Original file line numberDiff line numberDiff line change
@@ -217,81 +217,77 @@ fig.update_layout(annotations=annotations)
217217
fig.show()
218218
```
219219

220-
# 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+
```
221225
import pandas as pd
226+
import plotly.graph_objects as go
222227
223228
data = {
224229
"Category": ["Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"],
225-
"Strongly Agree": [40, 35, 50, 30, 60],
226230
"Somewhat Agree": [30, 25, 40, 20, 49],
227-
"Neutral": [15, 10, 20, 10, 30],
231+
"Strongly Agree": [40, 35, 50, 30, 60],
228232
"Somewhat Disagree": [-20, -15, -25, -10, -30],
229233
"Strongly Disagree": [-10, -50, -15, -15,-20]
230234
}
231235
df = pd.DataFrame(data)
232-
233-
import plotly.graph_objects as go
236+
print(df.columns)
234237
235238
fig = go.Figure()
236239
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+
}
245246
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
253251
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+
}
261258
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+
))
269270
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-
))
277271
278272
fig.update_layout(
279-
title="User Feedback on Online Education Services",
273+
title="Reactions to the statement, 'The service met your expectations for':",
280274
xaxis=dict(
281275
title="Number of Responses",
282276
zeroline=True, # Ensure there's a zero line for divergence
283277
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]
284283
),
285-
yaxis=dict(title=""),
284+
yaxis_title = "",
286285
barmode='relative', # Allows bars to diverge from the center
287286
plot_bgcolor="white",
288-
height=600,
289-
width=800
290287
)
291288
292289
fig.show()
293-
294-
# Reference https://plotly.com/python/horizontal-bar-charts/#basic-horizontal-bar-chart
290+
```
295291

296292
### Bar Chart with Line Plot
297293

0 commit comments

Comments
 (0)