Bird
Raised Fist0
Matplotlibdata~10 mins

Dumbbell charts in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the matplotlib plotting library.

Matplotlib
import [1] as plt
Drag options to blanks, or click blank then click option'
Aseaborn
Bmatplotlib.pyplot
Cpandas
Dnumpy
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong library like numpy or pandas instead of matplotlib.pyplot.
Forgetting to use 'as plt' for shorthand.
2fill in blank
medium

Complete the code to create a figure and axis for plotting.

Matplotlib
fig, ax = plt.[1]()
Drag options to blanks, or click blank then click option'
Asubplots
Bfigure
Cplot
Dshow
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.plot() which only plots data but does not create axes.
Using plt.figure() which creates a figure but not axes.
3fill in blank
hard

Fix the error in the code to plot two points connected by a line for a dumbbell chart.

Matplotlib
ax.plot([1, 2], [[1], 5], marker='o', color='blue')
Drag options to blanks, or click blank then click option'
A7
B5
C1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same y-value twice, which plots a horizontal line.
Using a single number instead of a list of two numbers.
4fill in blank
hard

Fill both blanks to create a dumbbell chart connecting two sets of values for categories.

Matplotlib
ax.hlines(y=[1], xmin=[2], xmax=[1], color='gray', linewidth=2)
Drag options to blanks, or click blank then click option'
A[1, 2, 3, 4]
B[5, 6, 7, 8]
C[0, 1, 2, 3]
D[4, 5, 6, 7]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same list for y and xmin, which results in zero-length lines.
Swapping xmin and xmax values.
5fill in blank
hard

Fill all three blanks to complete the dumbbell chart code with points and lines.

Matplotlib
ax.scatter([1], [2], color='red', label='Start')
ax.scatter([3], [2], color='blue', label='End')
ax.hlines(y=[2], xmin=[1], xmax=[3], color='gray')
Drag options to blanks, or click blank then click option'
A[1, 2, 3, 4]
B[5, 6, 7, 8]
D[0, 1, 2, 3]
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up xmin and xmax values.
Using different y values for scatter and lines.

Practice

(1/5)
1. What is the main purpose of a dumbbell chart in data visualization?
easy
A. To compare two related values for each category using connected points
B. To show the distribution of a single variable using bars
C. To display the relationship between two continuous variables as a scatter plot
D. To visualize hierarchical data using nested rectangles

Solution

  1. Step 1: Understand dumbbell chart structure

    Dumbbell charts show two points per category connected by a line to highlight differences or changes.
  2. Step 2: Compare with other chart types

    Bar charts show single values, scatter plots show relationships, and treemaps show hierarchies, so they don't fit dumbbell chart purpose.
  3. Final Answer:

    To compare two related values for each category using connected points -> Option A
  4. Quick Check:

    Dumbbell chart = compare two values [OK]
Hint: Dumbbell charts connect two points per category [OK]
Common Mistakes:
  • Confusing dumbbell charts with bar charts
  • Thinking dumbbell charts show distributions
  • Mixing up scatter plots with dumbbell charts
2. Which matplotlib functions are typically used together to create a dumbbell chart?
easy
A. bar and plot
B. pie and bar
C. hist and scatter
D. hlines and scatter

Solution

  1. Step 1: Identify dumbbell chart components

    Dumbbell charts use horizontal lines to connect two points and scatter points to mark values.
  2. Step 2: Match matplotlib functions

    hlines draws horizontal lines, scatter plots points, so these two create dumbbell charts.
  3. Final Answer:

    hlines and scatter -> Option D
  4. Quick Check:

    Lines + points = hlines & scatter [OK]
Hint: Use hlines for lines and scatter for points [OK]
Common Mistakes:
  • Using bar instead of hlines for connecting lines
  • Confusing hist with scatter
  • Trying pie charts for dumbbell visualization
3. What will be the output of this code snippet creating a dumbbell chart?
import matplotlib.pyplot as plt
categories = ['A', 'B']
start = [1, 3]
end = [4, 5]
plt.hlines(y=categories, xmin=start, xmax=end, color='gray')
plt.scatter(start, categories, color='blue')
plt.scatter(end, categories, color='red')
plt.show()
medium
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

  1. 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.
  2. Step 2: Confirm scatter commands also fail

    plt.scatter(start, categories) and subsequent scatter require numeric y-values, leading to the same error.
  3. Final Answer:

    An error because categories cannot be used as y-values -> Option C
  4. 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:
import matplotlib.pyplot as plt
categories = ['X', 'Y']
start = [2, 4]
end = [5, 7]
plt.hlines(y=categories, xmin=start, xmax=end)
plt.scatter(start, categories)
plt.scatter(end, categories)
plt.show()
medium
A. No error; code runs correctly
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

  1. 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).
  2. 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).
  3. Final Answer:

    Error because categories are strings; convert categories to numeric indices -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Use plt.hlines with y as products, xmin as sales_2022, xmax as sales_2023, and plt.scatter for points -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard