A. A horizontal dumbbell chart with two categories showing blue and red points connected by gray lines
B. A vertical bar chart with categories on x-axis and values on y-axis
C. An error because categories cannot be used as y-values
D. A scatter plot with categories on y-axis and points only at start values
Solution
Step 1: Analyze hlines command
plt.hlines(y=categories, xmin=start, xmax=end, color='gray') fails because y-coordinates must be numeric, but categories=['A', 'B'] are strings. Raises ValueError: could not convert string to float.
Step 2: Confirm scatter commands also fail
plt.scatter(start, categories) and subsequent scatter require numeric y-values, leading to the same error.
Final Answer:
An error because categories cannot be used as y-values -> Option C
Quick Check:
Strings as y-values in hlines/scatter -> error [OK]
Hint: hlines and scatter require numeric y-positions; map categories to indices [OK]
Common Mistakes:
Assuming strings work directly as y-values
Expecting horizontal dumbbell chart output
Confusing with bar chart output
4. Identify the error in this dumbbell chart code and select the fix:
B. Error because categories are strings; convert categories to numeric indices
C. Error because scatter requires numeric y-values; swap x and y in scatter
D. Error due to missing color argument in hlines
Solution
Step 1: Check data types for plotting
hlines and scatter both require numeric y-values. Passing string categories as y causes ValueError (cannot convert string to float).
Step 2: Fix by using numeric indices
Create y_pos = list(range(len(categories))), use y=y_pos in hlines and scatters, then plt.yticks(y_pos, categories).
Final Answer:
Error because categories are strings; convert categories to numeric indices -> Option B
Quick Check:
Categories (str) -> numeric indices [OK]
Hint: Use numeric y_pos = range(len(categories)); set labels with yticks [OK]
Common Mistakes:
Assuming hlines and scatter accept strings equally
Ignoring axis data type requirements
Missing that scatter needs numeric coordinates
5. You have sales data for two years for 4 products: products = ['P1', 'P2', 'P3', 'P4'], sales_2022 = [100, 150, 200, 130], sales_2023 = [120, 140, 210, 160]. How would you create a dumbbell chart to compare sales changes using matplotlib?
hard
A. Use plt.hlines with y as products, xmin as sales_2022, xmax as sales_2023, and plt.scatter for points
B. Use plt.bar for both years side by side
C. Use plt.plot with products on x-axis and sales on y-axis
D. Use plt.scatter only with sales_2023 values
Solution
Step 1: Understand dumbbell chart requirements
We need to show two values per product connected by a line to compare sales in 2022 and 2023.
Step 2: Map data to plotting functions
hlines draws lines between sales_2022 and sales_2023 at y=products; scatter plots points for each year's sales.
Final Answer:
Use plt.hlines with y as products, xmin as sales_2022, xmax as sales_2023, and plt.scatter for points -> Option A
Quick Check:
Dumbbell = hlines + scatter with two values [OK]
Hint: Connect two year values with hlines; mark points with scatter [OK]
Common Mistakes:
Using bar charts instead of dumbbell charts
Plotting only one year's data
Not connecting points with lines
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.