0
0
Matplotlibdata~30 mins

Dumbbell charts in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Dumbbell charts
📖 Scenario: You are a data analyst at a fitness center. You want to compare the average weights lifted by two groups of people: beginners and advanced trainees. A dumbbell chart is a great way to show the difference between these two groups for each exercise.
🎯 Goal: Build a dumbbell chart using matplotlib to visually compare the average weights lifted by beginners and advanced trainees for different exercises.
📋 What You'll Learn
Create a dictionary with exercises as keys and tuples of (beginner weight, advanced weight) as values.
Create a list of exercises from the dictionary keys.
Extract beginner and advanced weights into separate lists.
Plot a dumbbell chart using matplotlib with lines connecting beginner and advanced weights for each exercise.
Label the axes and add a title.
💡 Why This Matters
🌍 Real World
Dumbbell charts are useful to compare two related values side-by-side for multiple categories, such as comparing performance before and after training.
💼 Career
Data analysts and scientists often create such charts to communicate differences clearly in reports and presentations.
Progress0 / 4 steps
1
Create the data dictionary
Create a dictionary called weights with these exact entries: 'Squat': (50, 100), 'Bench Press': (40, 90), 'Deadlift': (60, 120), 'Overhead Press': (30, 70), 'Barbell Row': (45, 95).
Matplotlib
Need a hint?

Use curly braces to create a dictionary. Each key is a string for the exercise, and each value is a tuple with two numbers.

2
Prepare lists for plotting
Create a list called exercises from the keys of weights. Then create two lists called beginner_weights and advanced_weights by extracting the first and second elements of the tuples in weights respectively.
Matplotlib
Need a hint?

Use list(weights.keys()) to get the exercises. Use list comprehensions to get beginner and advanced weights.

3
Plot the dumbbell chart
Import matplotlib.pyplot as plt. Use plt.hlines to draw horizontal lines between beginner_weights and advanced_weights for each exercise index. Then use plt.scatter to plot the beginner and advanced weights as points. Set the y-axis ticks to exercises.
Matplotlib
Need a hint?

Use plt.hlines to draw lines, and plt.scatter to plot points. Use range(len(exercises)) for y positions.

4
Display the chart
Add plt.show() to display the dumbbell chart.
Matplotlib
Need a hint?

Use plt.show() to display the plot window.