📖 Scenario: You work as a data analyst for a company that wants to understand the distribution of sales data across different regions. You will use two common plots: box plot and violin plot, to compare the sales data visually.
🎯 Goal: Create a Python program using matplotlib to plot both a box plot and a violin plot side by side for the sales data of three regions. This will help you see how these plots show data distribution differently.
📋 What You'll Learn
Create a dictionary called sales_data with three keys: 'North', 'South', and 'East' and their sales values as lists of numbers.
Create a list called regions containing the region names in order: 'North', 'South', 'East'.
Use matplotlib.pyplot to create a figure with two subplots side by side.
Plot a box plot of the sales data on the first subplot using ax1.boxplot().
Plot a violin plot of the sales data on the second subplot using ax2.violinplot().
Set the x-axis tick labels to the region names for both plots.
Add titles to each subplot: 'Box Plot' and 'Violin Plot'.
Display the plots using plt.show().
💡 Why This Matters
🌍 Real World
Data analysts often compare different visualizations to understand data distribution and variability across groups or regions.
💼 Career
Knowing how to create and interpret box plots and violin plots is essential for data scientists and analysts to communicate insights effectively.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'North': [250, 270, 300, 280, 260], 'South': [220, 210, 230, 240, 225], and 'East': [300, 320, 310, 305, 315].
Matplotlib
Hint
Use curly braces {} to create the dictionary and colons : to assign lists to each region key.
2
Create the regions list
Create a list called regions containing the strings 'North', 'South', and 'East' in this exact order.
Matplotlib
Hint
Use square brackets [] to create the list and separate the region names with commas.
3
Plot box plot and violin plot side by side
Import matplotlib.pyplot as plt. Create a figure and two subplots side by side using plt.subplots(1, 2, figsize=(10, 5)). On the first subplot ax1, plot a box plot of the sales data values using ax1.boxplot(). On the second subplot ax2, plot a violin plot of the sales data values using ax2.violinplot(). Set the x-axis tick labels to regions for both plots. Add titles 'Box Plot' and 'Violin Plot' to ax1 and ax2 respectively.
Matplotlib
Hint
Use list comprehension to get the sales values in order for plotting. Use set_xticklabels to label x-axis ticks. Remember violin plot x-ticks start at 1.
4
Display the plots
Add a line to display the plots using plt.show().
Matplotlib
Hint
Use plt.show() to display the figure with both plots.
Practice
(1/5)
1. Which of the following best describes the main difference between a box plot and a violin plot?
easy
A. Box plots show the full data distribution shape, while violin plots only show median and quartiles.
B. Box plots show summary statistics like median and quartiles, while violin plots show the full data distribution shape.
C. Both plots show only the mean and standard deviation of the data.
D. Violin plots are used only for categorical data, box plots only for numerical data.
Solution
Step 1: Understand box plot features
Box plots display median, quartiles, and outliers, summarizing key statistics.
Step 2: Understand violin plot features
Violin plots show the full distribution shape using a density curve, revealing data spread and modality.
Final Answer:
Box plots show summary statistics like median and quartiles, while violin plots show the full data distribution shape. -> Option B
Quick Check:
Box = summary stats, Violin = distribution shape [OK]
Hint: Box = stats summary, Violin = full distribution shape [OK]
Common Mistakes:
Confusing violin plots as only showing summary stats
Thinking box plots show full distribution shape
Assuming violin plots are for categorical data only
2. Which of the following is the correct way to create a violin plot using matplotlib for a list of values data?
easy
A. plt.plot.violin(data)
B. plt.boxplot(data)
C. plt.violin(data)
D. plt.violinplot(data)
Solution
Step 1: Recall matplotlib violin plot syntax
The correct function to create a violin plot is plt.violinplot().
Step 2: Check other options for correctness
plt.boxplot() creates box plots, plt.violin() and plt.plot.violin() are invalid functions.
Final Answer:
plt.violinplot(data) -> Option D
Quick Check:
Use plt.violinplot() for violin plots [OK]
Hint: Use plt.violinplot() to create violin plots [OK]
Common Mistakes:
Using plt.boxplot() instead of plt.violinplot()
Trying plt.violin() which does not exist
Using plt.plot.violin() which is invalid
3. Given the following code, what will the output plot show?
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
data = np.random.normal(0, 1, 100)
plt.boxplot(data)
plt.show()
medium
A. A plot showing the median, quartiles, and outliers of the data.
B. A plot showing the full distribution shape with density curves.
C. A scatter plot of all data points.
D. A line plot of the sorted data values.
Solution
Step 1: Identify the plot function used
The code uses plt.boxplot(data), which creates a box plot.
Step 2: Understand box plot output
Box plots display median, quartiles, and outliers, summarizing data distribution.
Final Answer:
A plot showing the median, quartiles, and outliers of the data. -> Option A
Quick Check:
plt.boxplot() = median, quartiles, outliers [OK]
Hint: plt.boxplot() shows median and quartiles [OK]
Common Mistakes:
Confusing boxplot with violin plot output
Expecting density curves from boxplot
Thinking it shows scatter or line plot
4. The following code is intended to plot a violin plot but produces an error. What is the error?
import matplotlib.pyplot as plt
data = [1, 2, 3, 4, 5]
plt.violin(data)
plt.show()
medium
A. The function plt.violin() does not exist in matplotlib.
B. The data list must be converted to a numpy array first.
C. plt.show() must be called before plt.violin().
D. The data list is too short to plot a violin plot.
Solution
Step 1: Check the function name used
The code calls plt.violin(), which is not a valid matplotlib function.
Step 2: Identify correct function for violin plots
The correct function is plt.violinplot(), not plt.violin().
Final Answer:
The function plt.violin() does not exist in matplotlib. -> Option A
Quick Check:
Use plt.violinplot(), plt.violin() causes error [OK]
Hint: Use plt.violinplot(), not plt.violin() [OK]
Common Mistakes:
Using plt.violin() instead of plt.violinplot()
Thinking data type causes error
Calling plt.show() too early
5. You have two groups of data and want to compare their distributions clearly. Which approach best uses both box and violin plots together in matplotlib?
hard
A. Plot box plots side by side for each group only.
B. Plot violin plots stacked vertically without box plots.
C. Overlay violin plots with box plots for each group side by side.
D. Plot only histograms for each group.
Solution
Step 1: Understand the goal of comparing distributions
Using both box and violin plots together helps see summary stats and full distribution shape.
Step 2: Identify best plotting method
Overlaying violin plots with box plots side by side for each group shows both views clearly for comparison.
Final Answer:
Overlay violin plots with box plots for each group side by side. -> Option C
Quick Check:
Combine box + violin side by side for best comparison [OK]
Hint: Overlay box and violin plots side by side for clear comparison [OK]
Common Mistakes:
Using only box or only violin plots alone
Stacking plots vertically without overlay
Using histograms which show less detail
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.