Skip to content
Advertisement

seaborn is not plotting within defined subplots

I am trying to plot two displots side by side with this code

JavaScript

It returns the following result (two empty subplots followed by one displot each on two lines)-

enter image description here

enter image description here

enter image description here

If I try the same code with violinplot, it returns result as expected

JavaScript

enter image description here

Why is displot returning a different kind of output and what can I do to output two plots on the same line?

Advertisement

Answer

  • 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.
  • It is applicable to any of the seaborn FacetGrid plots that there is no ax parameter. Use the equivalent axes-level plot.
  • Because the histogram of two different columns is desired, it’s easier to use histplot.
  • See How to plot in multiple subplots for a number of different ways to plot into maplotlib.pyplot.subplots
  • Also review seaborn histplot and displot output doesn’t match
  • Tested in seaborn 0.11.1 & matplotlib 3.4.2
JavaScript

Imports and DataFrame Sample

JavaScript

Axes Level Plot

  • With the data in a wide format, use sns.histplot
JavaScript

enter image description here

Figure Level Plot

  • With the dataframe in a long format, use displot
JavaScript

Multiple DataFrames

  • If there are multiple dataframes, they can be combined with pd.concat, and use .assign to create an identifying 'source' column, which can be used for row=, col=, or hue=
JavaScript
Advertisement