Challenge - 5 Problems
Bar Chart Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Bar chart with custom bar width
What is the width of each bar in the resulting bar chart from this code?
Matplotlib
import matplotlib.pyplot as plt x = [1, 2, 3] heights = [4, 5, 6] plt.bar(x, heights, width=0.5) plt.show()
Attempts:
2 left
💡 Hint
Look at the width parameter in plt.bar function.
✗ Incorrect
The width parameter controls the width of each bar. Here it is set to 0.5, so each bar is half a unit wide.
❓ data_output
intermediate2:00remaining
Positions of grouped bars
Given two groups of bars plotted side by side with width 0.4, what are the x positions of the bars in the second group?
Matplotlib
import numpy as np import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] x = np.arange(len(labels)) width = 0.4 plt.bar(x - width/2, [1, 2, 3], width=width) plt.bar(x + width/2, [4, 5, 6], width=width) plt.show() positions_second_group = x + width/2 print(positions_second_group)
Attempts:
2 left
💡 Hint
Positions are shifted by half the bar width to the right for the second group.
✗ Incorrect
The second group bars are positioned at x + width/2, which shifts them 0.2 units right if width=0.4.
❓ visualization
advanced2:00remaining
Effect of bar width on bar overlap
Which bar width will cause the bars to overlap when plotted at positions [0, 1, 2]?
Matplotlib
import matplotlib.pyplot as plt x = [0, 1, 2] heights = [3, 4, 5] plt.bar(x, heights, width=1.2) plt.show()
Attempts:
2 left
💡 Hint
Bars overlap if width is greater than the distance between bar centers.
✗ Incorrect
Bars are centered at positions 0,1,2. Distance between centers is 1. Width 1.2 is greater than 1, so bars overlap.
🧠 Conceptual
advanced1:30remaining
Understanding bar alignment parameter
What does setting the 'align' parameter to 'edge' do in plt.bar compared to the default 'center'?
Attempts:
2 left
💡 Hint
Think about where the bar starts relative to the x coordinate.
✗ Incorrect
With align='edge', the x positions mark the left edge of each bar, not the center.
🔧 Debug
expert2:30remaining
Fixing incorrect bar positions in grouped bar chart
Given this code, which option correctly fixes the bar positions so the two groups do not overlap?
Matplotlib
import numpy as np import matplotlib.pyplot as plt labels = ['X', 'Y', 'Z'] x = np.arange(len(labels)) width = 0.3 plt.bar(x, [5, 7, 9], width=width) plt.bar(x, [6, 8, 10], width=width) plt.show()
Attempts:
2 left
💡 Hint
To avoid overlap, shift bars left and right by half the width.
✗ Incorrect
Option B correctly shifts the two groups left and right by half the bar width, so they sit side by side without overlap.