0
0
Matplotlibdata~20 mins

Diverging bar charts in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Diverging Bar Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple diverging bar chart code

What will be the output of this Python code using matplotlib to create a diverging bar chart?

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

labels = ['A', 'B', 'C', 'D']
values = np.array([3, -2, 5, -1])
colors = ['green' if x > 0 else 'red' for x in values]

plt.bar(labels, values, color=colors)
plt.axhline(0, color='black')
plt.show()
AA line chart connecting points A, B, C, D with values 3, -2, 5, -1.
BA bar chart with all bars in blue, some going up and some going down, with no horizontal line.
CA scatter plot with points at (A,3), (B,-2), (C,5), (D,-1) colored green and red.
DA bar chart with bars for A and C going up in green, bars for B and D going down in red, and a black horizontal line at zero.
Attempts:
2 left
💡 Hint

Look at how colors are assigned based on positive or negative values and the use of axhline.

data_output
intermediate
1:30remaining
Number of bars above and below zero

Given the following data and code for a diverging bar chart, how many bars will be above zero and how many below zero?

Matplotlib
import numpy as np
values = np.array([10, -5, 0, 3, -7, 2])
AAbove zero: 3 bars; Below zero: 2 bars
BAbove zero: 4 bars; Below zero: 2 bars
CAbove zero: 3 bars; Below zero: 3 bars
DAbove zero: 2 bars; Below zero: 3 bars
Attempts:
2 left
💡 Hint

Count values strictly greater than zero and strictly less than zero. Zero is neither.

visualization
advanced
2:00remaining
Identify the correct diverging bar chart visualization

Which of the following code snippets produces a diverging bar chart with labels on the y-axis and horizontal bars diverging from zero?

Aplt.scatter(labels, values, color='red')
Bplt.barh(labels, values, color=['red' if v < 0 else 'blue' for v in values]); plt.axvline(0, color='black')
Cplt.plot(labels, values, color='blue')
Dplt.bar(labels, values, color=['red' if v < 0 else 'blue' for v in values]); plt.axhline(0, color='black')
Attempts:
2 left
💡 Hint

Think about which function creates horizontal bars and where the zero line should be.

🔧 Debug
advanced
1:30remaining
Identify the error in diverging bar chart code

What error will this code raise?

import matplotlib.pyplot as plt
values = [4, -3, 2, -1]
colors = ['green' if x > 0 else 'red' for x in values]
plt.bar(range(len(values)), values, color=colors)
plt.axhline(0, color='black')
plt.show()
ANo error; the code runs and shows a diverging bar chart.
BTypeError because colors list length does not match bars.
CValueError because plt.bar expects labels, not range indices.
DNameError because 'plt' is not imported.
Attempts:
2 left
💡 Hint

Check if the colors list length matches the number of bars and if imports are correct.

🚀 Application
expert
2:30remaining
Calculate net positive and negative sums from diverging bar chart data

Given the following data representing changes in sales (positive and negative), what are the total positive and total negative sums?

sales_changes = [15, -7, 0, 8, -3, -5, 12]
ATotal positive sum: 27; Total negative sum: -15
BTotal positive sum: 35; Total negative sum: -10
CTotal positive sum: 35; Total negative sum: -15
DTotal positive sum: 27; Total negative sum: -10
Attempts:
2 left
💡 Hint

Add only positive numbers for positive sum, and only negative numbers for negative sum.