0
0
Matplotlibdata~20 mins

Vertical bar chart with plt.bar in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
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 vertical bar chart code
What will be the height of the bars in the vertical bar chart produced by this code?
Matplotlib
import matplotlib.pyplot as plt

x = ['A', 'B', 'C']
y = [5, 3, 7]
plt.bar(x, y)
plt.show()
A[5, 7, 3]
B[3, 5, 7]
C[7, 5, 3]
D[5, 3, 7]
Attempts:
2 left
💡 Hint
Look at the y values passed to plt.bar as heights.
data_output
intermediate
1:30remaining
Number of bars in the chart
How many bars will be displayed by this code snippet?
Matplotlib
import matplotlib.pyplot as plt

labels = ['X', 'Y', 'Z', 'W']
values = [10, 20, 15, 5]
plt.bar(labels, values)
plt.show()
A4
B2
C5
D3
Attempts:
2 left
💡 Hint
Count the number of labels or values given.
visualization
advanced
1:30remaining
Identify the bar colors in the chart
Given this code, what color will the bars be?
Matplotlib
import matplotlib.pyplot as plt

categories = ['Cat1', 'Cat2']
heights = [8, 12]
plt.bar(categories, heights, color='green')
plt.show()
AGreen
BRed
CBlue
DYellow
Attempts:
2 left
💡 Hint
Check the color argument in plt.bar.
🔧 Debug
advanced
2:00remaining
Error type from mismatched data lengths
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C']
values = [1, 2]
plt.bar(labels, values)
plt.show()
ATypeError: unsupported operand type(s)
BValueError: x and height must be the same length
CIndexError: list index out of range
DNo error, chart displays with missing bars
Attempts:
2 left
💡 Hint
Check if the number of labels matches the number of values.
🚀 Application
expert
2:30remaining
Calculate total bar height from grouped data
You have this data and code. What is the total height of all bars combined?
Matplotlib
import matplotlib.pyplot as plt

fruits = ['Apple', 'Banana', 'Cherry']
counts = [4, 7, 3]
plt.bar(fruits, counts)
plt.show()
A10
B7
C14
D4
Attempts:
2 left
💡 Hint
Add all the values in the counts list.