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评论94阅读模式
英文:

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. import seaborn.objects as so
  2. num_samples = 2000
  3. normal_distr = st.norm(1,1)
  4. sp = so.Plot()
  5. for s_n in range(10,num_samples,400):
  6. sample_normal = normal_distr.rvs(s_n)
  7. sp = sp.add(so.Line(), so.KDE(), x=sample_normal, color=s_n, label=s_n)
  8. 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)

  1. import scipy.stats as st
  2. import seaborn.objects as so
  3. num_samples = 2000
  4. normal_distr = st.norm(1,1)
  5. sp = so.Plot()
  6. for s_n in range(10,num_samples,400):
  7. sample_normal = normal_distr.rvs(s_n)
  8. sp = sp.add(so.Line(),so.KDE(),x=sample_normal,color=s_n)
  9. 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:

  1. import scipy.stats as st
  2. import seaborn as sns
  3. import matplotlib.pyplot as plt
  4. num_samples = 2000
  5. normal_distr = st.norm(1,1)
  6. for s_n in range(10,num_samples,400):
  7. sample_normal = normal_distr.rvs(s_n)
  8. sns.kdeplot(x=sample_normal, label=s_n)
  9. 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参数,因为它是设计用来使用的

  1. import scipy.stats as st
  2. import seaborn.objects as so
  3. import pandas as pd
  4. num_samples = 2000
  5. normal_distr = st.norm(1, 1)
  6. df = pd.concat([
  7. pd.DataFrame(
  8. {'sn': str(s_n),
  9. 'values': normal_distr.rvs(s_n)}
  10. )
  11. for s_n in range(10, num_samples, 400)
  12. ])

这会看起来像这样:

  1. sn values
  2. 0 10 0.976926
  3. 1 10 -0.501831
  4. 2 10 1.748071
  5. 3 10 0.968493
  6. 4 10 0.593531
  7. ... ... ...
  8. 1605 1610 0.311484
  9. 1606 1610 1.332424
  10. 1607 1610 1.531519
  11. 1608 1610 1.240953
  12. 1609 1610 -0.793144

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

  1. 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:

  1. import scipy.stats as st
  2. import seaborn.objects as so
  3. import pandas as pd
  4. num_samples = 2000
  5. normal_distr = st.norm(1,1)
  6. df = pd.concat([
  7. pd.DataFrame(
  8. {'sn': str(s_n),
  9. 'values': normal_distr.rvs(s_n)}
  10. )
  11. for s_n in range(10,num_samples,400)
  12. ])

This would look like this:

  1. sn values
  2. 0 10 0.976926
  3. 1 10 -0.501831
  4. 2 10 1.748071
  5. 3 10 0.968493
  6. 4 10 0.593531
  7. ... ... ...
  8. 1605 1610 0.311484
  9. 1606 1610 1.332424
  10. 1607 1610 1.531519
  11. 1608 1610 1.240953
  12. 1609 1610 -0.793144

Then printing can be done in a single line:

  1. 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:

确定