How to Plot Seaborn Plots Within Defined Matplotlib Subplots?

Patricia Arquette
Release: 2024-11-01 18:02:02
Original
310 people have browsed it

How to Plot Seaborn Plots Within Defined Matplotlib Subplots?

seaborn is not Plotting within Defined Subplots

Many seaborn plots can be plotted in subplots with the matplotlib.pyplot.subplots function, in the same way that regular matplotlib plots can be plotted. However, some functions have some limitation, such as having no ax parameter

The Deprecation of seaborn.distplot

Prior to version 0.11, the seaborn.distplot function was used to plot many different kind of distributions. This function has been deprecated with seaborn 0.11

seaborn.distplot() has been DEPRECATED in seaborn 0.11 and is replaced with the following:

displot(), a figure-level function with a similar flexibility over the kind of plot to draw. This is a FacetGrid, and does not have the ax parameter, so it will not work with matplotlib.pyplot.subplots.

histplot(), an axes-level function for plotting histograms, including with kernel density smoothing. This does have the ax parameter, so it will work with matplotlib.pyplot.subplots.
Copy after login

Finding the Correct Function

For any seaborn function that has no ax parameter, there is a corresponding axes-level function that can be used instead. To find the correct function, you can refer to the seaborn documentation for the figure-level plot to find the appropriate axes-level plot function.

Here is a list of figure-level plots that do not have an ax parameter:

  • relplot
  • displot
  • catplot

Plotting Different Plots on the Same Line

In this case, the goal is to plot two different histograms on the same row. Since displot is a figure-level function, and does not have an ax parameter, it cannot be used with matplotlib.pyplot.subplots. In this case, the correct function to use would be histplot, which is an axes-level function that does have an ax parameter.

Here is an example using histplot to plot two different histograms on the same row:

<code class="python">import seaborn as sns
import matplotlib.pyplot as plt

# load data
penguins = sns.load_dataset("penguins", cache=False)

# select the columns to be plotted
cols = ['bill_length_mm', 'bill_depth_mm']

# create the figure and axes
fig, axes = plt.subplots(1, 2)
axes = axes.ravel()  # flattening the array makes indexing easier

for col, ax in zip(cols, axes):
    sns.histplot(data=penguins[col], kde=True, stat='density', ax=ax)

fig.tight_layout()
plt.show()</code>
Copy after login

This will produce a figure with two histograms plotted on the same row.

Plotting Different Plots from Multiple DataFrames

If you have multiple dataframes, you can combine them using pandas pd.concat, and then use the assign method to create an identifying 'source' column, which can be used for specifying row= or col=, or as a hue parameter

<code class="python"># list of dataframe
lod = [df1, df2, df3]

# create one dataframe with a new 'source' column to use for row, col, or hue
df = pd.concat((d.assign(source=f'df{i}') for i, d in enumerate(lod, 1)), ignore_index=True)</code>
Copy after login

You can then use this combined dataframe to plot a variety of different plots using seaborn.

For more information, see the following resources:

  • [Seaborn Documentation](https://seaborn.pydata.org/)
  • [Plotting Subplots with Matplotlib](https://matplotlib.org/stable/tutorials/intermediate/subplots.html)
  • [Seaborn and Pandas Integration](https://seaborn.pydata.org/tutorial/using_pandas.html)

The above is the detailed content of How to Plot Seaborn Plots Within Defined Matplotlib Subplots?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!