'seaborn plot diffrent histogram and distrubtion on the same plot
I want to compare two distributions- one from real data- just plot histogram of cases and function of date and the other from predict model- plot the distribution. I have two codes, one for each distribution:
only KDE without hist-
ax=sns.displot(PLT2['DATE'],kind="kde") plt.xticks(rotation=90, fontsize=10) ax.set(xlim=(datetime.date(2013, 1, 1), datetime.date(2013, 12, 31)))
histogram from real data-
ax=sns.displot(df['DATE'].sort_values(),stat="density") plt.xticks(rotation=90, fontsize=10) plt.show()
I want to show those two on the same plot. I tried this code but in return 2 different plots:
ax1=sns.displot(df_2013['DATE'].sort_values(),stat="density")
ax2=sns.displot(PLT2['DATE'],kind="kde")
plt.xticks(rotation=90, fontsize=10)
ax1.set(xlim=(datetime.date(2013, 1, 1), datetime.date(2013, 12, 31)))
ax2.set(xlim=(datetime.date(2013, 1, 1), datetime.date(2013, 12, 31)))
plt.show()
thanks for helping
Solution 1:[1]
You need to define the figure first and add subplots to it.
with sns.axes_style("whitegrid"):
fig = plt.figure(figsize=(15,10))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.plot(df1['Column1'])
ax2.plot(df2['Column1'])
This way you can also plot on the same axis and overlay your plots if you want to. Or you can make plots align under each other in seperate subplots.
Here is an example of superimposed distplots that might be useful to you.
fig = plt.figure(figsize=(7.5,7.5))
ax1 = fig.add_subplot(211)
sns.distplot(Fnormal,ax = ax1, label="Normal Distribution from Filter");
sns.distplot(FilteredReturns, ax =ax1, label="Filtered Returns");
ax1.set_title('Comparison of Filtered Returns and Normal Distribution')
ax1.legend()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
