@@ -217,6 +217,82 @@ fig.update_layout(annotations=annotations)
217
217
fig.show()
218
218
```
219
219
220
+ # Q2. Example 1: Butterfly chart/diverging bar chart-1
221
+ import pandas as pd
222
+
223
+ data = {
224
+ "Category": [ "Content Quality", "Instructor Effectiveness", "Ease of Use", "Customer Support", "Value for Money"] ,
225
+ "Strongly Agree": [ 40, 35, 50, 30, 60] ,
226
+ "Somewhat Agree": [ 30, 25, 40, 20, 49] ,
227
+ "Neutral": [ 15, 10, 20, 10, 30] ,
228
+ "Somewhat Disagree": [ -20, -15, -25, -10, -30] ,
229
+ "Strongly Disagree": [ -10, -50, -15, -15,-20]
230
+ }
231
+ df = pd.DataFrame(data)
232
+
233
+ import plotly.graph_objects as go
234
+
235
+ fig = go.Figure()
236
+
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
+ ))
245
+
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
+ ))
253
+
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
+ ))
261
+
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
+ ))
269
+
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
+
278
+ fig.update_layout(
279
+ title="User Feedback on Online Education Services",
280
+ xaxis=dict(
281
+ title="Number of Responses",
282
+ zeroline=True, # Ensure there's a zero line for divergence
283
+ zerolinecolor="black",
284
+ ),
285
+ yaxis=dict(title=""),
286
+ barmode='relative', # Allows bars to diverge from the center
287
+ plot_bgcolor="white",
288
+ height=600,
289
+ width=800
290
+ )
291
+
292
+ fig.show()
293
+
294
+ # Reference https://plotly.com/python/horizontal-bar-charts/#basic-horizontal-bar-chart
295
+
220
296
### Bar Chart with Line Plot
221
297
222
298
``` python
@@ -335,4 +411,4 @@ fig.show()
335
411
336
412
### Reference
337
413
338
- See more examples of bar charts and styling options [ here] ( https://plotly.com/python/bar-charts/ ) .<br > See https://plotly.com/python/reference/bar/ for more information and chart attribute options!
414
+ See more examples of bar charts and styling options [ here] ( https://plotly.com/python/bar-charts/ ) .<br > See https://plotly.com/python/reference/bar/ for more information and chart attribute options!
0 commit comments