Why Are My Seaborn `displot` Subplots Empty?

DDD
Release: 2024-10-29 22:26:02
Original
887 people have browsed it

Why Are My Seaborn `displot` Subplots Empty?

seaborn.displot Not Plotting Within Defined Subplots

When attempting to plot two distributions side-by-side using sns.displot, users may encounter empty subplots followed by the expected displot on subsequent lines. This behavior is due to the deprecation of sns.distplot in favor of the more flexible displot and histplot functions.

Figure-Level vs. Axes-Level Plots

seaborn.displot is a figure-level function that lacks an ax parameter, while sns.histplot is an axes-level function that does have an ax parameter. This means that displot cannot be used with matplotlib.pyplot.subplots, while histplot can be used to visualize two plots on the same line.

To resolve the issue, you should use sns.histplot for your desired purpose. Here's an example:

<code class="python">fig, (ax1, ax2) = plt.subplots(1, 2)
sns.histplot(x=X_train['Age'], hue=y_train, ax=ax1)
sns.histplot(x=X_train['Fare'], hue=y_train, ax=ax2)</code>
Copy after login

Important Considerations

  • If you have a wide-format DataFrame and wish to plot histograms of multiple columns, it is recommended to use the axes-level sns.histplot function.
  • To flatten an axes array, you can use the ravel method.
  • When using displot with a long-format DataFrame, the common_bins and common_norm arguments should be set to False to avoid common binning and normalization of data.

Multiple DataFrames

If you want to plot distributions from multiple dataframes, you can:

  • Concatenate them using pd.concat.
  • Add a new column using assign to uniquely identify each dataframe's source.
  • Use the row, col, or hue parameters to differentiate between the plots.

The above is the detailed content of Why Are My Seaborn `displot` Subplots Empty?. 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
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!