0
0
Matplotlibdata~30 mins

Violin plot with plt.violinplot in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a Violin Plot with plt.violinplot
📖 Scenario: You are a data analyst working with a small dataset of exam scores from two classes. You want to visualize the distribution of scores to understand how students performed in each class.
🎯 Goal: Build a violin plot using plt.violinplot to compare the score distributions of two classes.
📋 What You'll Learn
Create a list of scores for two classes
Set up a figure and axis using matplotlib
Use plt.violinplot to plot the score distributions
Label the x-axis with class names
Display the plot
💡 Why This Matters
🌍 Real World
Violin plots help compare distributions of data, like exam scores or sales numbers, to see patterns and differences clearly.
💼 Career
Data analysts and scientists use violin plots to visualize data distributions and communicate insights effectively.
Progress0 / 4 steps
1
Create the exam scores data
Create two lists called class1_scores and class2_scores with these exact values: class1_scores = [55, 60, 65, 70, 75, 80, 85] and class2_scores = [50, 55, 60, 65, 70, 75, 80, 85, 90].
Matplotlib
Need a hint?

Use square brackets to create lists with the exact numbers given.

2
Set up the plot figure and axis
Import matplotlib.pyplot as plt. Then create a figure and axis using fig, ax = plt.subplots().
Matplotlib
Need a hint?

Use import matplotlib.pyplot as plt and then plt.subplots() to create the plot area.

3
Create the violin plot with plt.violinplot
Use ax.violinplot to plot the two score lists together. Pass a list containing class1_scores and class2_scores as the data argument.
Matplotlib
Need a hint?

Pass a list containing both score lists to ax.violinplot.

4
Label the plot and display it
Set the x-axis tick labels to ["Class 1", "Class 2"] using ax.set_xticks([1, 2]) and ax.set_xticklabels(["Class 1", "Class 2"]). Then call plt.show() to display the plot.
Matplotlib
Need a hint?

Use ax.set_xticks and ax.set_xticklabels to label the classes. Then call plt.show() to see the plot.