0
0
Matplotlibdata~20 mins

Lollipop charts in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lollipop Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA vertical lollipop chart with stems from zero to values for categories A, B, C, D
BA horizontal bar chart with bars for categories A, B, C, D
CA scatter plot with points at (categories, values) but no stems
DA line plot connecting points for categories A, B, C, D
Attempts:
2 left
💡 Hint
Look at the plt.stem function and what it draws.
data_output
intermediate
1: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()
A1
B0
C3
D15
Attempts:
2 left
💡 Hint
Count how many data points are plotted.
🔧 Debug
advanced
2: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()
ANo error, chart displays correctly
BTypeError: categories must be numeric
CSyntaxError: invalid syntax
DValueError: x and y must have same length
Attempts:
2 left
💡 Hint
Check if the lengths of categories and values match.
visualization
advanced
2: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()
AThe baseline line will be red and solid under the stems
BThe stems will be red instead of default color
CThe markers at data points will be red circles
DThe chart will have no baseline line visible
Attempts:
2 left
💡 Hint
basefmt controls the baseline line style and color.
🚀 Application
expert
2: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?
A
plt.stem(labels, values, basefmt=' ', use_line_collection=True)
plt.gca().invert_yaxis()
plt.show()
B
plt.stem(values, labels, basefmt=' ', orientation='horizontal', use_line_collection=True)
plt.show()
C
plt.stem(values, labels, basefmt=' ', use_line_collection=True)
plt.show()
D
plt.stem(labels, values, basefmt=' ', orientation='horizontal', use_line_collection=True)
plt.show()
Attempts:
2 left
💡 Hint
Check the orientation parameter and axis order for horizontal stem plots.