Challenge - 5 Problems
Lollipop Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a basic lollipop chart code
What will be the output of this Python code that creates a lollipop chart using matplotlib?
Matplotlib
import matplotlib.pyplot as plt categories = ['A', 'B', 'C', 'D'] values = [4, 7, 1, 8] plt.stem(categories, values, basefmt=" ", use_line_collection=True) plt.show()
Attempts:
2 left
💡 Hint
Look at the plt.stem function and what it draws.
✗ Incorrect
The plt.stem function creates vertical lines (stems) from a baseline to the data points, with markers at the data points, forming a lollipop chart.
❓ data_output
intermediate1:30remaining
Number of stems in a lollipop chart
Given this code, how many stems (lines) will the lollipop chart display?
Matplotlib
import matplotlib.pyplot as plt labels = ['X', 'Y', 'Z'] values = [10, 5, 15] plt.stem(labels, values, basefmt=" ", use_line_collection=True) plt.show()
Attempts:
2 left
💡 Hint
Count how many data points are plotted.
✗ Incorrect
Each data point corresponds to one stem line in the lollipop chart, so the number of stems equals the number of values.
🔧 Debug
advanced2:00remaining
Identify the error in lollipop chart code
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt categories = ['A', 'B', 'C'] values = [5, 10] plt.stem(categories, values, basefmt=" ", use_line_collection=True) plt.show()
Attempts:
2 left
💡 Hint
Check if the lengths of categories and values match.
✗ Incorrect
The plt.stem function requires x and y to have the same length. Here categories has 3 items but values has 2, causing a ValueError.
❓ visualization
advanced2:00remaining
Effect of changing baseline in lollipop chart
What will be the visual effect of setting basefmt='r-' in plt.stem for this code?
Matplotlib
import matplotlib.pyplot as plt labels = ['P', 'Q', 'R'] values = [3, 6, 9] plt.stem(labels, values, basefmt='r-', use_line_collection=True) plt.show()
Attempts:
2 left
💡 Hint
basefmt controls the baseline line style and color.
✗ Incorrect
The basefmt parameter sets the style of the baseline line. 'r-' means a red solid line under the stems.
🚀 Application
expert2:30remaining
Choosing correct code for horizontal lollipop chart
Which option produces a horizontal lollipop chart with categories on y-axis and values on x-axis?
Attempts:
2 left
💡 Hint
Check the orientation parameter and axis order for horizontal stem plots.
✗ Incorrect
To create a horizontal lollipop chart, plt.stem must have orientation='horizontal' and x, y data swapped: x=values, y=labels. Option B correctly sets orientation and axis order.