0
0
Matplotlibdata~30 mins

Before-after comparison plots in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Before-After Comparison Plots
📖 Scenario: You work in a health clinic. You want to compare patients' blood pressure before and after a treatment.
🎯 Goal: Create a simple plot that shows blood pressure values before and after treatment for each patient. This helps to see if the treatment worked.
📋 What You'll Learn
Create a dictionary with patient names and their blood pressure before treatment
Create a dictionary with patient names and their blood pressure after treatment
Use matplotlib to plot before and after values side by side for each patient
Label the plot clearly with title, axis labels, and legend
💡 Why This Matters
🌍 Real World
Before-after comparison plots are used in health, marketing, and many fields to show changes clearly.
💼 Career
Data scientists often create comparison plots to communicate results of experiments or treatments.
Progress0 / 4 steps
1
Create blood pressure data dictionaries
Create a dictionary called bp_before with these exact entries: 'Anna': 140, 'Ben': 130, 'Cara': 150, 'Dan': 135. Then create another dictionary called bp_after with these exact entries: 'Anna': 130, 'Ben': 125, 'Cara': 140, 'Dan': 128.
Matplotlib
Need a hint?

Use curly braces {} to create dictionaries with the exact patient names and values.

2
Prepare patient names list for plotting
Create a list called patients that contains the patient names in this exact order: 'Anna', 'Ben', 'Cara', 'Dan'.
Matplotlib
Need a hint?

Use square brackets [] to create a list with the patient names in the given order.

3
Plot before and after blood pressure values
Import matplotlib.pyplot as plt. Create two lists called before_values and after_values that contain the blood pressure values from bp_before and bp_after for each patient in patients. Then use plt.bar to plot the before values shifted left by 0.2 and after values shifted right by 0.2 on the x-axis positions. Use patients as x-axis labels.
Matplotlib
Need a hint?

Use list comprehension to get values for each patient. Use plt.bar twice with shifted x positions for before and after bars.

4
Add labels and show the plot
Add a title 'Blood Pressure Before and After Treatment' using plt.title. Label the x-axis as 'Patients' and y-axis as 'Blood Pressure' using plt.xlabel and plt.ylabel. Add a legend with plt.legend(). Set the x-axis ticks to range(len(patients)) with labels patients using plt.xticks. Finally, display the plot with plt.show(). This will show the before and after blood pressure comparison clearly.
Matplotlib
Need a hint?

Use plt.title, plt.xlabel, plt.ylabel, plt.legend, plt.xticks, and plt.show to complete the plot.