Using seaborn python library, I am trying to make several density plots overlapping each other in the same figure and I want to color/label each of the lines. Using seaborn objects interface I am able to make the density plots within a for loop. But I cannot add color/label to each density plot.
I understand that there are other ways e.g., I create a dataframe with all the data and corresponding labels first and then pass it to seaborn plot(). But I was just wondering if below code (using seaborn objects interface) could work with some modifications. Please advise.
- Plot using Seaborn objects
Code:
Here I am setting color=s_n which is the number of samples that I drew from the normal distribution. I want to label each density plot with the number of samples (please also the see the desired plot towards the end of post)
import scipy.stats as st
import seaborn.objects as so
num_samples = 2000
normal_distr = st.norm(1,1)
sp = so.Plot()
for s_n in range(10,num_samples,400):
sample_normal = normal_distr.rvs(s_n)
sp = sp.add(so.Line(),so.KDE(),x=sample_normal,color=s_n)
sp.show()
The plots looks like this and it does not color/label each density line separately.
- Desired Plot
If I directly use seaborn kdeplot, I can get the desired plot (below). But I was just wondering if I can use seaborn objects instead of direct kdeplot
Code using kdeplot:
import scipy.stats as st
import seaborn as sns
import matplotlib.pyplot as plt
num_samples = 2000
normal_distr = st.norm(1,1)
for s_n in range(10,num_samples,400):
sample_normal = normal_distr.rvs(s_n)
sns.kdeplot(x=sample_normal, label=s_n)
plt.legend()
The (desired) plot: