Use seaborn object interface to plot overlapping density plots, added inside a for loop, each having its own color/label shown in a legend

huangapple go评论64阅读模式
英文:

Use seaborn object interface to plot overlapping density plots, added inside a for loop, each having its own color/label shown in a legend

问题

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, label=s_n)
sp.show()
英文:

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.

Use seaborn object interface to plot overlapping density plots, added inside a for loop, each having its own color/label shown in a legend

  • 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:

Use seaborn object interface to plot overlapping density plots, added inside a for loop, each having its own color/label shown in a legend

答案1

得分: 1

我猜这里的诀窍是准备好你的数据框,这样你就可以放弃循环并使用color参数,因为它是设计用来使用的

import scipy.stats as st
import seaborn.objects as so
import pandas as pd

num_samples = 2000
normal_distr = st.norm(1, 1)

df = pd.concat([
    pd.DataFrame(
        {'sn': str(s_n),
         'values': normal_distr.rvs(s_n)}
    )
    for s_n in range(10, num_samples, 400)
])

这会看起来像这样:

        sn    values
0       10  0.976926
1       10 -0.501831
2       10  1.748071
3       10  0.968493
4       10  0.593531
...    ...       ...
1605  1610  0.311484
1606  1610  1.332424
1607  1610  1.531519
1608  1610  1.240953
1609  1610 -0.793144

然后打印可以在一行内完成:

so.Plot(df, x='values').add(so.Line(), so.KDE(common_norm=False), color='sn').show()
英文:

I guess the trick here would be to prepare your df so that you can forgo the loop and use the color kwarg as it's meant to be used:

import scipy.stats as st
import seaborn.objects as so
import pandas as pd

num_samples = 2000
normal_distr = st.norm(1,1)

df = pd.concat([
    pd.DataFrame(
        {'sn': str(s_n),
        'values': normal_distr.rvs(s_n)}
        )
    for s_n in range(10,num_samples,400)
])

This would look like this:

        sn    values
0       10  0.976926
1       10 -0.501831
2       10  1.748071
3       10  0.968493
4       10  0.593531
...    ...       ...
1605  1610  0.311484
1606  1610  1.332424
1607  1610  1.531519
1608  1610  1.240953
1609  1610 -0.793144

Then printing can be done in a single line:

so.Plot(df, x='values').add(so.Line(), so.KDE(common_norm=False), color='sn').show()

Output:

Use seaborn object interface to plot overlapping density plots, added inside a for loop, each having its own color/label shown in a legend

huangapple
  • 本文由 发表于 2023年4月20日 01:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定