Bird
Raised Fist0
Matplotlibdata~20 mins

Customizing Seaborn plots with Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Seaborn Matplotlib Customizer
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Seaborn plot customization code?

Consider the following Python code using Seaborn and Matplotlib:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style="darkgrid")
data = sns.load_dataset("tips")
plot = sns.scatterplot(x="total_bill", y="tip", data=data)
plot.set_title("Total Bill vs Tip")
plot.set_xlabel("Bill Amount")
plot.set_ylabel("Tip Amount")
plt.show()

What will be the title of the plot?

Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style="darkgrid")
data = sns.load_dataset("tips")
plot = sns.scatterplot(x="total_bill", y="tip", data=data)
plot.set_title("Total Bill vs Tip")
plot.set_xlabel("Bill Amount")
plot.set_ylabel("Tip Amount")
plt.show()
ATotal Bill vs Tip
BBill Amount vs Tip Amount
CScatterplot of Tips
DNo title will be shown
Attempts:
2 left
💡 Hint

Look at the set_title method call on the plot object.

data_output
intermediate
2:00remaining
How many ticks will be on the x-axis after this customization?

Given this code snippet:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme()
data = sns.load_dataset("iris")
plot = sns.boxplot(x="species", y="sepal_length", data=data)
plot.set_xticks([0, 1, 2])
plt.show()

How many tick labels will appear on the x-axis?

Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme()
data = sns.load_dataset("iris")
plot = sns.boxplot(x="species", y="sepal_length", data=data)
plot.set_xticks([0, 1, 2])
plt.show()
A1
B4
C0
D3
Attempts:
2 left
💡 Hint

Check the list passed to set_xticks.

visualization
advanced
2:00remaining
Which option produces a Seaborn line plot with a red dashed line?

Which code snippet will produce a Seaborn line plot with the line colored red and dashed?

Asns.lineplot(x=[1,2,3], y=[4,5,6], style='--', color='red')
Bsns.lineplot(x=[1,2,3], y=[4,5,6], linestyle='--', color='red')
Csns.lineplot(x=[1,2,3], y=[4,5,6], line_style='dashed', color='red')
Dsns.lineplot(x=[1,2,3], y=[4,5,6], linestyle='dotted', color='red')
Attempts:
2 left
💡 Hint

Check the correct parameter name for line style in Seaborn.

🔧 Debug
advanced
2:00remaining
What error does this code raise when customizing a Seaborn plot?

Examine this code:

import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("diamonds")
plot = sns.histplot(data["carat"])
plot.set_xlabel("Carat Weight")
plot.set_ylabel("Frequency")
plot.set_title("Carat Distribution")
plot.set_xticks([0.5, 1.0, 1.5, 2.0])
plt.show()

What error will this code raise?

Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

data = sns.load_dataset("diamonds")
plot = sns.histplot(data["carat"])
plot.set_xlabel("Carat Weight")
plot.set_ylabel("Frequency")
plot.set_title("Carat Distribution")
plot.set_xticks([0.5, 1.0, 1.5, 2.0])
plt.show()
ANo error, plot displays correctly
BTypeError: set_xticks() argument must be a list of integers
CAttributeError: 'AxesSubplot' object has no attribute 'set_xticks'
DValueError: Invalid tick positions
Attempts:
2 left
💡 Hint

Check if set_xticks is a valid method for the plot object.

🚀 Application
expert
3:00remaining
After customizing a Seaborn barplot, what is the color of the bars?

Given this code:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style="whitegrid")
data = sns.load_dataset("penguins")
plot = sns.barplot(x="species", y="flipper_length_mm", data=data, color="skyblue")
for bar in plot.patches:
    bar.set_facecolor("orange")
plt.show()

What color will the bars appear in the final plot?

Matplotlib
import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme(style="whitegrid")
data = sns.load_dataset("penguins")
plot = sns.barplot(x="species", y="flipper_length_mm", data=data, color="skyblue")
for bar in plot.patches:
    bar.set_facecolor("orange")
plt.show()
AGreen
BSkyblue
COrange
DDefault Seaborn color
Attempts:
2 left
💡 Hint

Consider the effect of the loop changing bar face colors after initial color setting.

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

  1. Step 1: Understand Seaborn's default features

    Seaborn provides nice default plots but limited direct customization options.
  2. Step 2: Role of Matplotlib in customization

    Matplotlib functions let you add titles, labels, grids, and adjust figure size to improve clarity.
  3. Final Answer:

    To customize titles, labels, and figure size for better clarity -> Option C
  4. 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

  1. Step 1: Identify how Seaborn plots integrate with Matplotlib

    Seaborn plots are Matplotlib objects, so Matplotlib functions like plt.title() work.
  2. Step 2: Check the syntax for setting titles

    Matplotlib's plt.title() sets the title for the current plot.
  3. Final Answer:

    plt.title('My Title') -> Option D
  4. 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

  1. Step 1: Analyze axis labeling commands

    plt.xlabel('X Axis') and plt.ylabel('Y Axis') add labels to X and Y axes respectively.
  2. Step 2: Analyze grid command

    plt.grid(True) enables grid lines on the plot.
  3. Final Answer:

    Scatter plot with labeled X and Y axes and grid lines visible -> Option A
  4. Quick Check:

    Labels and grid enabled by plt commands [OK]
Hint: plt.xlabel/ylabel add labels; plt.grid(True) shows grid [OK]
Common Mistakes:
  • Assuming grid is off by default
  • Forgetting plt.show() to display plot
  • Confusing sns and plt labeling functions
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

  1. 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.
  2. 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.
  3. Final Answer:

    The figure size should be set using plt.figure(figsize=(10,5)) before plotting -> Option B
  4. 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

  1. Step 1: Set figure size before plotting

    Use plt.figure(figsize=(12,6)) to set the plot size before creating the plot.
  2. 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.
  3. 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.
  4. 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
  5. 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]
Common Mistakes:
  • Passing figsize to sns.lineplot (not supported)
  • Using sns.set_figsize (does not exist)
  • Calling sns.title or sns.xlabel (wrong library)