Seaborn is not plotting within defined subplots in Python

Seaborn is not plotting within defined subplots in Python is a problem which occurs when .distplot is Deprecated.

This post is a guide from me to other fellow developers, the goal here is showing you why you are getting this error and how you can get rid of it in an efficient way.

Explaining the issue : Seaborn is not plotting within defined subplots in Python

The problem happens when we use .distplot in Seaborn while not knowing that it is Deprecated.

Let us try to replicate this issue, let us say we have to plot 3 plots with seaborn.

First, we start with the following line of code

                                                                       #
fig,(ax1,ax2,ax3) = plt.subplots(1,2,3)
                                                                       #

Then we use .displot to plot each plot. Using the code bellow

                                                                       #
sns.displot(x =X_train['King'], hue=y_train, ax=ax1)
sns.displot(x =X_train['Queen'], hue=y_train, ax=ax2)
sns.displot(x =X_train['Warrior'], hue=y_train, ax=ax3)
                                                                       #

Our goal was to plot the plots side by side but that result is FAR from the GOAL

We get three empty subplots and then three lines with a subplot on each line.

To solve the problem above, I have a solution which worked for me, bellow is a detailed explanation of said solution.

Solution : Replacing .displot with .histplot

First, .distplot has been replaced by other ways to plot using Seaborn. You can visit the Seaborn documentation for more details.

So, what is the alternative to .displot?

After a lot of research and testing, I arrived at the conclusion that the best way to replace .displot is using .histplot

In this case, the code bellow.

                                                                       #
fig,(ax1,ax2,ax3) = plt.subplots(1,2,3)
sns.displot(x =X_train['King'], hue=y_train, ax=ax1)
sns.displot(x =X_train['Queen'], hue=y_train, ax=ax2)
sns.displot(x =X_train['Warrior'], hue=y_train, ax=ax3)
                                                                       #

Should be replaced with the code bellow, we are just replacing .displot with .histplot

                                                                       #
fig,(ax1,ax2,ax3) = plt.subplots(1,2,3)
sns.histplot(x =X_train['King'], hue=y_train, ax=ax1)
sns.histplot(x =X_train['Queen'], hue=y_train, ax=ax2)
sns.histplot(x =X_train['Warrior'], hue=y_train, ax=ax3)
                                                                       #

That is enought to plot the three plots together side by side.

I hope the solution has been helpful, I hope you solved the issue Seaborn is not plotting within defined subplots in Python already, you can support us by donating to our Kofi account, this Blog is free to use by other developers, any help is appreciated and goes a long way.

Summing-up

I hope this article has been helpful and helped you solve this problem, the solution presented should be enough. I hope you continue coding and learning Python, issues are part of the journey of a developer even when we hate those problems, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/