Seaborn helps you make charts easily. Figure-level and axes-level functions decide how much control you get over the chart layout.
Seaborn figure-level vs axes-level in Matplotlib
Start learning this pattern below
Jump into concepts and practice - no test required
Figure-level function: seaborn.function_name(data=..., x=..., y=..., ...) Axes-level function: ax = matplotlib.pyplot.subplot() seaborn.function_name(data=..., x=..., y=..., ax=ax, ...)
Figure-level functions create the whole figure and manage layout automatically.
Axes-level functions draw on a specific part (axes) of an existing figure, giving more control.
import seaborn as sns sns.scatterplot(data=df, x='age', y='height')
import matplotlib.pyplot as plt fig, ax = plt.subplots() sns.scatterplot(data=df, x='age', y='height', ax=ax) plt.show()
This code shows both figure-level and axes-level histograms of penguin flipper lengths by species. The first plot is made by seaborn managing the figure. The second plot uses matplotlib to create the figure and axes, then seaborn draws on that axes.
import seaborn as sns import matplotlib.pyplot as plt # Sample data penguins = sns.load_dataset('penguins') # Figure-level plot: creates figure and axes automatically fig1 = sns.histplot(data=penguins, x='flipper_length_mm', hue='species', multiple='stack') plt.title('Figure-level: Flipper Length Distribution') plt.show() # Axes-level plot: create figure and axes first, then plot on axes fig2, ax2 = plt.subplots() sns.histplot(data=penguins, x='flipper_length_mm', hue='species', multiple='stack', ax=ax2) ax2.set_title('Axes-level: Flipper Length Distribution') plt.show()
Figure-level functions include: relplot, catplot, displot, pairplot, jointplot.
Axes-level functions include: scatterplot, histplot, boxplot, violinplot, kdeplot.
Use figure-level when you want quick plots with built-in layout.
Use axes-level when you want to combine plots or customize layout deeply.
Figure-level functions create the whole figure and manage layout automatically.
Axes-level functions draw on specific axes you create, giving more control.
Choose figure-level for quick, simple plots; axes-level for detailed customization.
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
