Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Customizing Seaborn plots with Matplotlib
📖 Scenario: You are analyzing sales data for a small store. You want to visualize the monthly sales using a bar plot. You will use Seaborn to create the plot and then customize it using Matplotlib to make it clearer and more attractive.
🎯 Goal: Create a Seaborn bar plot of monthly sales and customize it using Matplotlib by adding a title, axis labels, and changing the color of the bars.
📋 What You'll Learn
Create a dictionary called sales_data with months as keys and sales numbers as values
Create a list called highlight_months containing months to highlight
Use Seaborn to create a bar plot of the sales data
Use Matplotlib to add a title and axis labels
Change the color of bars for the months in highlight_months to a different color
💡 Why This Matters
🌍 Real World
Visualizing sales data helps businesses understand trends and make decisions. Customizing plots makes the information clearer and more appealing to stakeholders.
💼 Career
Data analysts and scientists often use Seaborn and Matplotlib to create and customize visualizations for reports and presentations.
Progress0 / 4 steps
1
DATA SETUP: Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: 'January': 150, 'February': 200, 'March': 170, 'April': 220, 'May': 190.
Matplotlib
Hint
Use curly braces {} to create a dictionary with the given month names as keys and sales numbers as values.
2
CONFIGURATION: Define months to highlight
Create a list called highlight_months containing the months 'February' and 'April' to highlight in the plot.
Matplotlib
Hint
Use square brackets [] to create a list with the two month names as strings.
3
CORE LOGIC: Create and customize the Seaborn bar plot
Import seaborn as sns and matplotlib.pyplot as plt. Create a bar plot using sns.barplot with months on the x-axis and sales on the y-axis from sales_data. Use a list comprehension to set the color of bars to 'orange' if the month is in highlight_months, otherwise 'skyblue'. Then add a title 'Monthly Sales' and axis labels 'Month' and 'Sales' using Matplotlib.
Matplotlib
Hint
Use list() to get keys and values from the dictionary. Use a list comprehension to create the colors list. Pass palette=colors to sns.barplot. Use plt.title, plt.xlabel, and plt.ylabel to add text.
4
OUTPUT: Display the customized plot
Use plt.show() to display the plot with the custom colors, title, and axis labels.
Matplotlib
Hint
Call plt.show() to display the plot window.
Practice
(1/5)
1. What is the main reason to use Matplotlib functions when working with Seaborn plots?
easy
A. To convert Seaborn plots into interactive web charts
B. To create Seaborn plots from scratch without Seaborn
C. To customize titles, labels, and figure size for better clarity
D. To replace Seaborn's default color palette
Solution
Step 1: Understand Seaborn's default features
Seaborn provides nice default plots but limited direct customization options.
Step 2: Role of Matplotlib in customization
Matplotlib functions let you add titles, labels, grids, and adjust figure size to improve clarity.
Final Answer:
To customize titles, labels, and figure size for better clarity -> Option C
Quick Check:
Matplotlib customizes Seaborn plots [OK]
Hint: Matplotlib adds polish to Seaborn plots [OK]
Common Mistakes:
Thinking Matplotlib replaces Seaborn plotting
Believing Matplotlib changes Seaborn colors only
Confusing customization with interactivity
2. Which of the following is the correct way to set a title on a Seaborn plot using Matplotlib?
easy
A. sns_plot.title('My Title')
B. >edoc/<)'eltiT yM'(eltit.tolp_sns>edoc<
C. sns.set_title('My Title')
D. plt.title('My Title')
Solution
Step 1: Identify how Seaborn plots integrate with Matplotlib
Seaborn plots are Matplotlib objects, so Matplotlib functions like plt.title() work.
Step 2: Check the syntax for setting titles
Matplotlib's plt.title() sets the title for the current plot.
Final Answer:
plt.title('My Title') -> Option D
Quick Check:
Use plt.title() to set titles [OK]
Hint: Use plt.title() to add titles on Seaborn plots [OK]
Common Mistakes:
Trying to call title() directly on sns object
Using sns.set_title which does not exist
Confusing plot object methods with Matplotlib functions
3. What will be the effect of the following code on a Seaborn plot?
import seaborn as sns
import matplotlib.pyplot as plt
sns_plot = sns.scatterplot(x=[1,2,3], y=[4,5,6])
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.grid(True)
plt.show()
medium
A. Scatter plot with labeled X and Y axes and grid lines visible
B. Scatter plot without axis labels and no grid lines
C. Scatter plot with grid lines but no axis labels
D. Scatter plot with axis labels but grid lines hidden
Solution
Step 1: Analyze axis labeling commands
plt.xlabel('X Axis') and plt.ylabel('Y Axis') add labels to X and Y axes respectively.
Step 2: Analyze grid command
plt.grid(True) enables grid lines on the plot.
Final Answer:
Scatter plot with labeled X and Y axes and grid lines visible -> Option A
4. Identify the error in the code below that tries to change the figure size of a Seaborn plot:
import seaborn as sns
import matplotlib.pyplot as plt
sns_plot = sns.barplot(x=[1,2,3], y=[4,5,6])
sns_plot.figure(figsize=(10,5))
plt.show()
medium
A. The correct method is sns_plot.set_figsize(10,5)
B. The figure size should be set using plt.figure(figsize=(10,5)) before plotting
C. sns.barplot does not support figure size changes
D. Figure size cannot be changed after plotting
Solution
Step 1: Understand how to set figure size in Matplotlib
Figure size is set by creating a figure with plt.figure(figsize=(width,height)) before plotting.
Step 2: Identify the mistake in the code
Calling sns_plot.figure(figsize=(10,5)) is incorrect because 'figure' is not a method of the plot object.
Final Answer:
The figure size should be set using plt.figure(figsize=(10,5)) before plotting -> Option B
Quick Check:
Set figure size with plt.figure() before plotting [OK]
Hint: Use plt.figure(figsize=...) before plotting [OK]
Common Mistakes:
Calling figure() on plot object
Trying to set figure size after plot creation
Using non-existent set_figsize method
5. You want to create a Seaborn line plot with a custom figure size of 12x6 inches, a title 'Sales Over Time', X-axis label 'Month', Y-axis label 'Sales', and grid lines visible. Which code snippet correctly achieves this?
hard
A.
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
sns.lineplot(x=[1,2,3], y=[100,200,300])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()
B.
import seaborn as sns
import matplotlib.pyplot as plt
sns.lineplot(x=[1,2,3], y=[100,200,300], figsize=(12,6))
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()
C.
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_figsize(12,6)
sns.lineplot(x=[1,2,3], y=[100,200,300])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()
D.
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
sns.lineplot(x=[1,2,3], y=[100,200,300])
sns.title('Sales Over Time')
sns.xlabel('Month')
sns.ylabel('Sales')
sns.grid(True)
plt.show()
Solution
Step 1: Set figure size before plotting
Use plt.figure(figsize=(12,6)) to set the plot size before creating the plot.
Step 2: Use Matplotlib functions for title, labels, and grid
Matplotlib functions plt.title(), plt.xlabel(), plt.ylabel(), and plt.grid(True) customize the plot after creation.
Step 3: Verify code correctness
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
sns.lineplot(x=[1,2,3], y=[100,200,300])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()
correctly uses plt.figure and Matplotlib functions; other options misuse parameters or functions.
Final Answer:
plt.figure(figsize=(12,6)); sns.lineplot(); plt.title('Sales Over Time'); plt.xlabel('Month'); plt.ylabel('Sales'); plt.grid(True) -> Option A
Quick Check:
Set figure size with plt.figure, customize with plt functions [OK]
Hint: Set size with plt.figure, customize with plt.title/labels/grid [OK]