Seaborn figure-level vs axes-level in Matplotlib - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When using Seaborn with matplotlib, it is important to understand how the time to create plots grows as data size increases.
We want to see how figure-level and axes-level plotting functions differ in their time cost as data grows.
Analyze the time complexity of these two Seaborn plotting approaches.
import seaborn as sns
import matplotlib.pyplot as plt
# Figure-level plot
sns.relplot(data=large_data, x="x", y="y", kind="scatter")
# Axes-level plot
fig, ax = plt.subplots()
sns.scatterplot(data=large_data, x="x", y="y", ax=ax)
plt.show()
The first creates a figure-level plot managing figure and axes internally. The second creates an axes-level plot on an existing axes.
Look at what repeats as data size grows.
- Primary operation: Plotting each data point as a marker on the axes.
- How many times: Once per data point in the dataset.
As the number of data points increases, the number of markers to draw grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 markers drawn |
| 100 | 100 markers drawn |
| 1000 | 1000 markers drawn |
Pattern observation: The work grows directly with the number of points; doubling points doubles work.
Time Complexity: O(n)
This means the time to create the plot grows linearly with the number of data points.
[X] Wrong: "Figure-level plots are always slower because they manage more things."
[OK] Correct: Both figure-level and axes-level plots spend most time drawing points, so their time grows similarly with data size.
Understanding how plotting time grows helps you explain performance in data visualization tasks clearly and confidently.
What if we added a loop to create multiple subplots each with n data points? How would the time complexity change?
Practice
figure-level function in Seaborn?Solution
Step 1: Understand figure-level function role
Figure-level functions in Seaborn create the entire plot including figure and axes automatically.Step 2: Compare with axes-level functions
Axes-level functions only draw on existing axes and do not create the figure.Final Answer:
It creates a complete plot including figure and axes automatically. -> Option AQuick Check:
Figure-level = creates full plot [OK]
- Confusing figure-level with axes-level functions
- Thinking figure-level functions require manual axes creation
- Assuming axes-level functions create figures automatically
Solution
Step 1: Identify axes-level function usage
Axes-level functions likescatterplotcan accept anaxparameter to plot on existing axes.Step 2: Check options for correct syntax
sns.scatterplot(ax=ax, data=df, x='age', y='height') correctly passesax=axtoscatterplot, an axes-level function.Final Answer:
sns.scatterplot(ax=ax, data=df, x='age', y='height') -> Option BQuick Check:
Axes-level functions useax=parameter [OK]
- Using figure-level functions with ax= parameter (not supported)
- Confusing relplot (figure-level) with scatterplot (axes-level)
- Omitting ax= when plotting on existing axes
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('tips')
fig, ax = plt.subplots()
sns.scatterplot(data=df, x='total_bill', y='tip', ax=ax)
plt.title('Scatterplot on existing axes')
plt.show()Solution
Step 1: Analyze code creating figure and axes
The code creates a figure and axes withplt.subplots()and stores axes inax.Step 2: Understand scatterplot with ax parameter
scatterplotis axes-level and plots on the givenax. Title is set on the figure.Final Answer:
A scatterplot of total_bill vs tip on the existing axes with custom title -> Option CQuick Check:
Axes-level plot on existing axes = scatterplot with ax= [OK]
- Expecting scatterplot to create new figure automatically
- Thinking ax= causes error with scatterplot
- Assuming title applies only to figure-level plots
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('tips')
fig, ax = plt.subplots()
sns.relplot(data=df, x='total_bill', y='tip', ax=ax)
plt.show()Solution
Step 1: Check relplot function parameters
relplotis a figure-level function and does not accept anaxparameter.Step 2: Understand error cause
Passingax=axtorelplotcauses an error because it manages figure creation internally.Final Answer:
relplot does not accept ax parameter; it creates its own figure -> Option DQuick Check:
Figure-level functions ignore ax= and raise error if given [OK]
- Passing ax= to figure-level functions
- Confusing relplot with scatterplot usage
- Assuming plt.subplots() is incorrect here
Solution
Step 1: Understand figure-level vs axes-level functions
relplotis figure-level and creates its own figure;histplotandscatterplotare axes-level and can plot on existing axes.Step 2: Plan side-by-side plots
Creating subplots withplt.subplots()and plotting axes-level functions on each axes allows side-by-side plots in one figure.Step 3: Evaluate options
Usesns.histplot()on one axes andsns.scatterplot()on another axes created byplt.subplots(). correctly uses axes-level functions on subplots. Options A, B, and D misuse figure-level functions or ax= parameter.Final Answer:
Use sns.histplot() on one axes and sns.scatterplot() on another axes created by plt.subplots(). -> Option AQuick Check:
Axes-level functions + subplots = combined figure [OK]
- Trying to use relplot with ax= parameter
- Using multiple figure-level functions expecting one figure
- Mixing figure-level and axes-level functions on same axes
