0
0
Matplotlibdata~20 mins

Combining Seaborn and Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Seaborn-Matplotlib Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of combined Seaborn and Matplotlib plot commands

What will be the color of the line plotted by Matplotlib after the Seaborn scatter plot?

Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style('darkgrid')
plt.scatter([1, 2, 3], [4, 5, 6], color='red')
plt.plot([1, 2, 3], [6, 5, 4])
plt.show()
AThe line will be blue, the default Matplotlib color.
BThe line will be red, matching the scatter points color.
CThe line will be green, as Seaborn changes the default color cycle.
DThe line will not appear because Seaborn disables Matplotlib plotting.
Attempts:
2 left
💡 Hint

Seaborn sets styles but does not change Matplotlib's default line color unless specified.

data_output
intermediate
2:00remaining
Data output after combining Seaborn and Matplotlib plot commands

What is the number of lines plotted after running this code?

Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 2, 1]})
sns.lineplot(data=df, x='x', y='y')
plt.plot([1, 2, 3], [1, 2, 3])
lines = plt.gca().get_lines()
print(len(lines))
A3
B1
C2
D0
Attempts:
2 left
💡 Hint

Count all lines added by both Seaborn and Matplotlib.

visualization
advanced
2:00remaining
Effect of Seaborn style on Matplotlib histogram

Which option best describes the visual style of the histogram after applying Seaborn's whitegrid style?

Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

sns.set_style('whitegrid')
data = np.random.normal(size=100)
plt.hist(data, bins=10)
plt.show()
AHistogram bars are transparent with no background color.
BHistogram bars have grid lines behind them on a white background.
CHistogram bars have no grid lines and a black background.
DHistogram bars are plotted but the grid lines are removed.
Attempts:
2 left
💡 Hint

Recall what whitegrid style does in Seaborn.

🔧 Debug
advanced
2:00remaining
Identify the error when combining Seaborn and Matplotlib

What error will this code raise?

Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style('dark')
plt.bar([1, 2, 3], [4, 5])
plt.show()
AValueError: x and height must have the same length
BTypeError: set_style() got an unexpected keyword argument
CSyntaxError: invalid syntax in plt.bar call
DNo error, the plot will display correctly
Attempts:
2 left
💡 Hint

Check the lengths of the x and height lists passed to plt.bar.

🚀 Application
expert
3:00remaining
Combining Seaborn and Matplotlib for layered plots

You want to create a scatter plot with Seaborn and then add a horizontal line at y=0.5 using Matplotlib. Which code snippet correctly achieves this?

A
sns.scatterplot(x=[1,2,3], y=[0.4,0.6,0.8])
sns.axhline(y=0.5, color='red')
plt.show()
B
plt.scatter([1,2,3], [0.4,0.6,0.8])
sns.axhline(y=0.5, color='red')
plt.show()
C
plt.scatter([1,2,3], [0.4,0.6,0.8])
plt.axhline(y=0.5, color='red')
sns.show()
D
sns.scatterplot(x=[1,2,3], y=[0.4,0.6,0.8])
plt.axhline(y=0.5, color='red')
plt.show()
Attempts:
2 left
💡 Hint

Remember which library provides axhline and how to combine plots.