点状 Seaborn distplot

3
我有一个seaborn displot,看起来像这样: enter image description here 我想将一些线条变成虚线。我该如何实现? 我尝试了两种不同的linestyle方法,但都出现了错误。
#### approach 1
for x, m in x_list:
    sns.distplot(x, hist=False, label=m, linestyle='--')
#### approach 2
for x, m in x_list:
    sns.distplot(x, hist=False, label=m, kde_kws={'linestyle':'--'})

TypeError: distplot() got an unexpected keyword argument 'linestyle'
2个回答

5
第二种方法使用kde_kws={'linestyle':'--'}在seaborn 8.1中可以正常工作。也许你想要更新。
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

x_list = [np.random.rayleigh(1+i/2., size=35) for i in range(4)]

for x in x_list:
    sns.distplot(x, hist=False, kde_kws={'linestyle':'--'})

plt.show()

enter image description here


是的,我有7.1版本。由于限制,我无法升级,但感谢您的指引。 - ShikharDua
Seaborn没有任何限制。它是纯Python的。您可以使用pip安装它,也可以从GitHub下载。 - ImportanceOfBeingErnest

2
您可以通过 seaborn distplot 返回的轴来获取 Line2D 对象列表,使用 ax.lines。然后,使用循环遍历这些对象,并使用 set_linestyle 来设置所需的线条样式。
例如:
import seaborn as sns

x = np.random.randn(100)
ax = sns.distplot(x, hist=False)

[line.set_linestyle("--") for line in ax.lines] 

plt.show()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接