Overview of Seaborn Plotting Functions - Seaborn 0.13.2 Documentation
Overview of Seaborn Plotting Functions - Seaborn 0.13.2 Documentation
2 documentation
For example, the distributions module defines functions that specialize in representing the distribution of datapoints. This includes familiar methods like
the histogram:
penguins = sns.load_dataset("penguins")
sns.histplot(data=penguins, x="flipper_length_mm", hue="species", multiple="stack")
Along with similar, but perhaps less familiar, options such as kernel density estimation:
Functions within a module share a lot of underlying code and offer similar features that may not be present in other components of the library (such as
multiple="stack" in the examples above). They are designed to facilitate switching between different visual representations as you explore a dataset,
because different representations often have complementary strengths and weaknesses.
In addition to the different modules, there is a cross-cutting classification of seaborn functions as “axes-level” or “figure-level”. The examples above are
axes-level functions. They plot data onto a single matplotlib.pyplot.Axes object, which is the return value of the function.
In contrast, figure-level functions interface with matplotlib through a seaborn object, usually a FacetGrid , that manages the figure. Each module has a
single figure-level function, which offers a unitary interface to its various axes-level functions. The organization looks a bit like this:
For example, displot() is the figure-level function for the distributions module. Its default behavior is to draw a histogram, using the same code as
histplot() behind the scenes:
To draw a kernel density plot instead, using the same code as kdeplot() , select it using the kind parameter:
https://seaborn.pydata.org/tutorial/function_overview.html 2/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
You’ll notice that the figure-level plots look mostly like their axes-level counterparts, but there are a few differences. Notably, the legend is placed
outside the plot. They also have a slightly different shape (more on that shortly).
The most useful feature offered by the figure-level functions is that they can easily create figures with multiple subplots. For example, instead of
stacking the three distributions for each species of penguins in the same axes, we can “facet” them by plotting each distribution across the columns of
the figure:
The figure-level functions wrap their axes-level counterparts and pass the kind-specific keyword arguments (such as the bin size for a histogram) down
to the underlying function. That means they are no less flexible, but there is a downside: the kind-specific parameters don’t appear in the function
signature or docstrings. Some of their features might be less discoverable, and you may need to look at two different pages of the documentation
before understanding how to achieve a specific goal.
The axes-level functions call matplotlib.pyplot.gca() internally, which hooks into the matplotlib state-machine interface so that they draw their plots
on the “currently-active” axes. But they additionally accept an ax= argument, which integrates with the object-oriented interface and lets you specify
exactly where each plot should go:
https://seaborn.pydata.org/tutorial/function_overview.html 3/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
Nevertheless, it is possible to go beyond what the figure-level functions offer by accessing the matplotlib axes on the object that they return and adding
other elements to the plot that way:
tips = sns.load_dataset("tips")
g = sns.relplot(data=tips, x="total_bill", y="tip")
g.ax.axline(xy1=(10, 2), slope=.2, color="b", dashes=(5, 2))
https://seaborn.pydata.org/tutorial/function_overview.html 4/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
While convenient, this does add a bit of extra complexity, as you need to remember that this method is not part of the matplotlib API and exists only
when using a figure-level function.
When using a figure-level function, there are several key differences. First, the functions themselves have parameters to control the figure size (although
these are actually parameters of the underlying FacetGrid that manages the figure). Second, these parameters, height and aspect , parameterize the
size slightly differently than the width , height parameterization in matplotlib (using the seaborn parameters, width = height * aspect ). Most
importantly, the parameters correspond to the size of each subplot, rather than the size of the overall figure.
To illustrate the difference between these approaches, here is the default output of matplotlib.pyplot.subplots() with one subplot:
f, ax = plt.subplots()
A figure with multiple columns will have the same overall size, but the axes will be squeezed horizontally to fit in the space:
f, ax = plt.subplots(1, 2, sharey=True)
https://seaborn.pydata.org/tutorial/function_overview.html 5/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
In contrast, a plot created by a figure-level function will be square. To demonstrate that, let’s set up an empty plot by using FacetGrid directly. This
happens behind the scenes in functions like relplot() , displot() , or catplot() :
g = sns.FacetGrid(penguins)
When additional columns are added, the figure itself will become wider, so that its subplots have the same size and shape:
g = sns.FacetGrid(penguins, col="sex")
And you can adjust the size and shape of each subplot without accounting for the total number of rows and columns in the figure:
https://seaborn.pydata.org/tutorial/function_overview.html 6/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
The upshot is that you can assign faceting variables without stopping to think about how you’ll need to adjust the total figure size. A downside is that,
when you do want to change the figure size, you’ll need to remember that things work a bit differently than they do in matplotlib.
Advantages Drawbacks
On balance, the figure-level functions add some additional complexity that can make things more confusing for beginners, but their distinct features
give them additional power. The tutorial documentation mostly uses the figure-level functions, because they produce slightly cleaner plots, and we
generally recommend their use for most applications. The one situation where they are not a good choice is when you need to make a complex,
standalone figure that composes multiple different plot kinds. At this point, it’s recommended to set up the figure using matplotlib directly and to fill in
the individual components using axes-level functions.
jointplot() plots the relationship or joint distribution of two variables while adding marginal axes that show the univariate distribution of each one
separately:
https://seaborn.pydata.org/tutorial/function_overview.html 7/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
pairplot() is similar — it combines joint and marginal views — but rather than focusing on a single relationship, it visualizes every pairwise
combination of variables simultaneously:
sns.pairplot(data=penguins, hue="species")
https://seaborn.pydata.org/tutorial/function_overview.html 8/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
Behind the scenes, these functions are using axes-level functions that you have already met ( scatterplot() and kdeplot() ), and they also have a
kind parameter that lets you quickly swap in a different representation:
https://seaborn.pydata.org/tutorial/function_overview.html 9/10
19/04/2024 Overview of seaborn plotting functions — seaborn 0.13.2 documentation
https://seaborn.pydata.org/tutorial/function_overview.html 10/10