0
0
Matplotlibdata~20 mins

Why bar charts compare categories in Matplotlib - Challenge Your Understanding

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
What does this bar chart code output?
Look at this Python code using matplotlib. What will the bar chart show?
Matplotlib
import matplotlib.pyplot as plt
categories = ['Apples', 'Bananas', 'Cherries']
values = [10, 15, 7]
plt.bar(categories, values)
plt.show()
AA line chart connecting points for Apples, Bananas, Cherries
BA bar chart with three bars labeled Apples, Bananas, Cherries with heights 10, 15, 7
CA pie chart showing proportions of 10, 15, 7
DAn error because plt.bar needs numeric x values
Attempts:
2 left
💡 Hint
plt.bar can take category names as x values to label bars.
data_output
intermediate
1:30remaining
How many bars are shown in this chart?
Given this code, how many bars will appear in the bar chart?
Matplotlib
import matplotlib.pyplot as plt
fruits = ['Apple', 'Banana', 'Cherry', 'Date']
counts = [5, 8, 2, 6]
plt.bar(fruits, counts)
plt.show()
A3
B0
C4
D5
Attempts:
2 left
💡 Hint
Each category in the list gets one bar.
visualization
advanced
3:00remaining
Which option shows a grouped bar chart comparing two categories?
You want to compare sales of two products across three months using a grouped bar chart. Which code snippet creates this correctly?
A
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar']
product1 = [10, 15, 12]
product2 = [8, 14, 10]
import numpy as np
x = np.arange(len(months))
width = 0.35
plt.bar(x - width/2, product1, width, label='Product 1')
plt.bar(x + width/2, product2, width, label='Product 2')
plt.xticks(x, months)
plt.legend()
plt.show()
B
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar']
product1 = [10, 15, 12]
product2 = [8, 14, 10]
plt.bar(months, product1)
plt.bar(months, product2)
plt.show()
C
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar']
product1 = [10, 15, 12]
product2 = [8, 14, 10]
plt.plot(months, product1)
plt.plot(months, product2)
plt.show()
D
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar']
product1 = [10, 15, 12]
product2 = [8, 14, 10]
plt.bar(product1, months)
plt.bar(product2, months)
plt.show()
Attempts:
2 left
💡 Hint
Use numpy to shift bars left and right for grouping.
🧠 Conceptual
advanced
1:30remaining
Why are bar charts good for comparing categories?
Which reason best explains why bar charts are used to compare categories?
ABecause bars show exact numeric values better than any other chart
BBecause bar charts use colors to represent data trends over time
CBecause bar charts can only show one category at a time
DBecause bars make it easy to see differences in size between categories visually
Attempts:
2 left
💡 Hint
Think about how our eyes compare lengths.
🔧 Debug
expert
2:00remaining
What error does this bar chart code raise?
This code tries to plot a bar chart but fails. What error will it raise?
Matplotlib
import matplotlib.pyplot as plt
categories = ['A', 'B', 'C']
values = [5, 10]
plt.bar(categories, values)
plt.show()
AValueError: x and height must have the same size
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CIndexError: list index out of range
DNo error, it plots correctly
Attempts:
2 left
💡 Hint
Check if the categories and values lists have the same length.