Home > Article > Backend Development > How to Plot Seaborn Plots Within Defined Matplotlib 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
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.
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:
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>
This will produce a figure with two histograms plotted on the same row.
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>
You can then use this combined dataframe to plot a variety of different plots using seaborn.
For more information, see the following resources:
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!