0
0
Matplotlibdata~15 mins

Seaborn figure-level vs axes-level in Matplotlib - Trade-offs & Expert Analysis

Choose your learning style9 modes available
Overview - Seaborn figure-level vs axes-level
What is it?
Seaborn is a Python library that helps make beautiful charts easily. It has two main types of plotting functions: figure-level and axes-level. Figure-level functions create a whole plot with its own figure and axes, managing layout automatically. Axes-level functions draw on a specific part of a figure, letting you combine multiple plots in one figure.
Why it matters
Without knowing the difference, you might struggle to combine plots or control layout well. Figure-level functions simplify making complete plots quickly, while axes-level functions give you fine control to build complex visualizations. This helps you tell better stories with data and avoid messy or confusing charts.
Where it fits
Before this, you should know basic Python plotting with matplotlib and simple Seaborn plots. After this, you can learn advanced plot customization, combining multiple plots, and creating dashboards or reports with multiple charts.
Mental Model
Core Idea
Figure-level functions create entire plots managing figure and axes, while axes-level functions draw on existing axes for more control and combination.
Think of it like...
Imagine figure-level functions as ordering a full meal at a restaurant, where everything comes together on one plate. Axes-level functions are like cooking individual dishes yourself and arranging them on your own plates to create a custom meal.
┌───────────────────────────────┐
│        Figure-level plot       │
│  (creates figure + axes)       │
│   ┌───────────────────────┐   │
│   │      Single Axes       │   │
│   │  (plot drawn inside)   │   │
│   └───────────────────────┘   │
└───────────────────────────────┘

┌───────────────────────────────┐
│        Axes-level plot         │
│  (draws on existing axes)      │
│   ┌────────┐  ┌────────┐       │
│   │ Axes 1 │  │ Axes 2 │       │
│   │ plot   │  │ plot   │       │
│   └────────┘  └────────┘       │
└───────────────────────────────┘
Build-Up - 7 Steps
1
FoundationWhat is Seaborn plotting
🤔
Concept: Seaborn is a library to make statistical plots easier and prettier.
Seaborn builds on matplotlib and lets you create charts like scatter plots, histograms, and box plots with simple commands. It handles colors, styles, and layout to make your charts look good by default.
Result
You can create a basic plot with one line of code, like sns.scatterplot(data=df, x='age', y='height').
Understanding Seaborn's purpose helps you appreciate why it has different types of plotting functions.
2
FoundationMatplotlib figures and axes basics
🤔
Concept: Matplotlib plots are made of figures (the whole window) and axes (the plot area inside).
A figure is like a blank canvas. Axes are areas inside the figure where data is drawn. You can have one or many axes in a figure to show multiple plots side by side.
Result
You can create a figure and axes with plt.subplots(), then draw plots on axes.
Knowing figures and axes is key to understanding how Seaborn's two plot types differ.
3
IntermediateFigure-level functions create full plots
🤔Before reading on: do you think figure-level functions let you add multiple plots in one figure easily? Commit to your answer.
Concept: Figure-level functions create a new figure and axes internally and manage layout automatically.
Functions like sns.relplot(), sns.catplot(), and sns.pairplot() create complete plots. They return a FacetGrid object that controls the whole figure and can create multiple subplots (facets) based on data categories.
Result
Calling sns.relplot() creates a new figure with one or more axes, ready to display your data.
Understanding that figure-level functions manage the whole figure helps you use them for quick, complete visualizations.
4
IntermediateAxes-level functions draw on existing axes
🤔Before reading on: do you think axes-level functions create new figures automatically? Commit to your answer.
Concept: Axes-level functions draw plots on axes you provide or create, without managing the whole figure.
Functions like sns.scatterplot(), sns.boxplot(), and sns.histplot() draw on a single axes. You can create a matplotlib figure and axes yourself, then call these functions to draw on them. This lets you combine multiple plots in one figure.
Result
You can create a figure with multiple axes and draw different plots on each using axes-level functions.
Knowing axes-level functions give you control over where plots appear enables building complex multi-plot figures.
5
IntermediateCombining axes-level plots in one figure
🤔Before reading on: do you think you can combine multiple figure-level plots into one figure? Commit to your answer.
Concept: Axes-level functions let you combine multiple plots in one figure by drawing on different axes.
You create a matplotlib figure with multiple axes using plt.subplots(). Then call axes-level functions on each axes to draw different plots. This is useful for dashboards or comparing plots side by side.
Result
A single figure window with multiple different plots arranged as you want.
Understanding this helps you build custom layouts and combine different visualizations flexibly.
6
AdvancedFacetGrid and figure-level internals
🤔Before reading on: do you think FacetGrid is just a plot or also controls layout? Commit to your answer.
Concept: Figure-level functions return a FacetGrid object that manages figure, axes, and layout for multiple subplots.
FacetGrid creates a grid of axes based on data categories. It handles spacing, titles, and axis labels automatically. You can customize it by mapping plotting functions to the grid.
Result
A multi-plot figure with consistent layout and styling controlled by FacetGrid.
Knowing FacetGrid's role explains how figure-level functions simplify complex multi-plot creation.
7
ExpertMixing figure-level and axes-level plots carefully
🤔Before reading on: do you think you can safely add axes-level plots inside a figure-level plot's axes? Commit to your answer.
Concept: Figure-level plots manage their own figure and axes tightly, so mixing axes-level plots inside them requires care to avoid conflicts.
If you try to draw axes-level plots inside axes created by figure-level functions, you must use the FacetGrid's axes explicitly. Otherwise, you risk overwriting or breaking the layout. Understanding this helps when customizing or extending figure-level plots.
Result
You can extend figure-level plots with custom axes-level plots without breaking layout or style.
Knowing the internal management of figure-level plots prevents common bugs and layout issues in advanced visualizations.
Under the Hood
Figure-level functions create a matplotlib Figure and a grid of Axes internally, wrapped in a FacetGrid object. This object manages layout, spacing, and axis sharing. Axes-level functions take an existing Axes object and draw the plot on it without changing the figure structure. Figure-level functions handle multiple subplots automatically, while axes-level functions require manual figure and axes management.
Why designed this way?
Seaborn was designed to simplify common plotting tasks with figure-level functions for quick, complete plots. At the same time, it offers axes-level functions for flexibility and fine control. This dual design balances ease of use and customization, avoiding forcing users into one style.
┌───────────────────────────────┐
│       Figure-level plot        │
│  ┌─────────────────────────┐  │
│  │      FacetGrid object    │  │
│  │ ┌────────┐ ┌───────────┐│  │
│  │ │ Axes 1 │ │  Axes 2   ││  │
│  │ └────────┘ └───────────┘│  │
│  └─────────────────────────┘  │
└───────────────────────────────┘

┌───────────────────────────────┐
│       Axes-level plot          │
│  ┌────────┐                   │
│  │ Axes   │                   │
│  │ object │                   │
│  └────────┘                   │
└───────────────────────────────┘
Myth Busters - 3 Common Misconceptions
Quick: Do figure-level functions let you add plots to existing figures easily? Commit to yes or no.
Common Belief:Figure-level functions can be used to add plots to any existing figure or axes.
Tap to reveal reality
Reality:Figure-level functions always create a new figure and axes internally; they do not add to existing figures.
Why it matters:Trying to add figure-level plots to existing figures causes unexpected new windows or overwritten plots, confusing layout and wasting resources.
Quick: Do axes-level functions automatically manage figure size and layout? Commit to yes or no.
Common Belief:Axes-level functions handle figure size and layout automatically like figure-level functions.
Tap to reveal reality
Reality:Axes-level functions only draw on given axes; you must manage figure size and layout yourself.
Why it matters:Without manual layout management, axes-level plots can overlap or appear cramped, leading to poor visualization quality.
Quick: Can you mix figure-level and axes-level functions freely without issues? Commit to yes or no.
Common Belief:You can freely mix figure-level and axes-level functions in the same figure without problems.
Tap to reveal reality
Reality:Mixing them without care can break layout or cause plots to overwrite each other because figure-level functions tightly control their figure and axes.
Why it matters:Ignoring this leads to bugs and confusing plots, wasting time debugging layout problems.
Expert Zone
1
Figure-level functions return a FacetGrid object that can be further customized or mapped with axes-level functions for advanced control.
2
Axes-level functions can be used inside figure-level plots by accessing the FacetGrid's axes, but this requires understanding the internal structure.
3
Figure-level functions handle axis sharing and legend placement automatically, which axes-level functions do not, requiring manual handling.
When NOT to use
Avoid figure-level functions when you need precise control over multiple plots in one figure or want to combine different plot types manually. Instead, use axes-level functions with matplotlib's figure and axes management.
Production Patterns
In production, figure-level functions are used for quick exploratory analysis and standard reports. Axes-level functions are preferred for dashboards, complex multi-plot layouts, and when integrating plots into custom GUIs or web apps.
Connections
Matplotlib figure and axes
Builds-on
Understanding matplotlib's figure and axes concepts is essential to grasp how Seaborn's figure-level and axes-level functions operate and differ.
Dashboard design
Application
Knowing axes-level plotting helps build dashboards where multiple plots must be arranged precisely, similar to arranging widgets in a user interface.
Modular programming
Similar pattern
The separation of figure-level and axes-level functions mirrors modular design: high-level modules manage overall structure, while low-level modules handle details, improving flexibility and reuse.
Common Pitfalls
#1Trying to add a figure-level plot to an existing matplotlib figure expecting it to appear there.
Wrong approach:fig, ax = plt.subplots() sns.relplot(data=df, x='x', y='y', ax=ax)
Correct approach:sns.relplot(data=df, x='x', y='y') # Let relplot create its own figure # Or use axes-level function: fig, ax = plt.subplots() sns.scatterplot(data=df, x='x', y='y', ax=ax)
Root cause:Figure-level functions do not accept an 'ax' parameter and always create new figures, so passing ax is ignored or causes errors.
#2Using axes-level functions without creating a figure or axes first, expecting a plot to appear.
Wrong approach:sns.scatterplot(data=df, x='x', y='y') # No figure or axes created explicitly
Correct approach:fig, ax = plt.subplots() sns.scatterplot(data=df, x='x', y='y', ax=ax)
Root cause:Axes-level functions require an axes to draw on; without one, matplotlib uses the current axes which may not exist or be configured properly.
#3Mixing figure-level and axes-level functions without accessing FacetGrid axes, causing layout breakage.
Wrong approach:g = sns.relplot(data=df, x='x', y='y') sns.scatterplot(data=df2, x='x', y='y') # Not specifying axes
Correct approach:g = sns.relplot(data=df, x='x', y='y') sns.scatterplot(data=df2, x='x', y='y', ax=g.ax)
Root cause:Figure-level plots manage their own axes; drawing on default axes ignores the figure-level layout and causes overlapping or missing plots.
Key Takeaways
Seaborn has two main plot types: figure-level functions create complete plots managing figure and axes, while axes-level functions draw on existing axes for more control.
Figure-level functions are great for quick, multi-plot visualizations with automatic layout, returning a FacetGrid object to customize further.
Axes-level functions require you to create and manage matplotlib figures and axes, enabling complex multi-plot layouts and dashboards.
Mixing figure-level and axes-level functions requires understanding their internal management to avoid layout conflicts and bugs.
Knowing when to use each type helps you create clear, flexible, and professional data visualizations.