0

I am trying to overlay two seaborn displots. Following this question: How can I overlay two graphs in Seaborn? I did the following:

import matplotlib.pyplot as plt    
import seaborn as sns
fig, ax = plt.subplots()
sns.displot(df['x'], stat='density', ax=ax)
sns.displot(other_x_no_df, color='r', kind='kde', ax=ax)

However, this produces three separate plots. (1) an empty plot, (2) the first displot, (3) the second displot.

My question: What is wrong with the code displayed? How do I properly overlay the two displots?

shenflow
  • 283
  • 2
  • 11
  • 4
    Try switching from using displot() to histplot(). Displot is a figure-level function - when you use it, a new figure is created. On the contrary, histplot is an axes-level function. You can read more about it here: https://seaborn.pydata.org/tutorial/function_overview.html – nelyanne Mar 16 '21 at 11:19
  • 2
    You might want `kdeplot()` instead of `histplot()` (or combine both) in your case. Also note that using `ax=` on a figure level function such as `displot()` shows **"UserWarning: `displot` is a figure-level function and does not accept the ax= parameter. You may wish to try histplot."** – JohanC Mar 16 '21 at 11:28
  • May be [this answer](https://stackoverflow.com/questions/63895392/seaborn-displot-is-not-plotting-within-defined-subplots) your question ? – Jay Patel Mar 16 '21 at 11:35
  • @nelyanne and JohanC, thanks. That worked. – shenflow Mar 16 '21 at 13:39

0 Answers0