简体   繁体   English

Dotted Seaborn distplot

[英]Dotted Seaborn distplot

I have a seaborn displot which looks like this 我有一个看起来像这样的seaborn displot 在此输入图像描述

I want to make some lines as dotted. 我想做一些点缀的线条。 How can I achieve this ? 我怎样才能做到这一点? I tried to use linestyle in 2 different ways and got error 我尝试以两种不同的方式使用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'

You can get a list of the Line2D objects from the axes returned by the seaborn distplot using ax.lines . 您可以使用ax.lines从seaborn distplot返回的轴中获取Line2D对象的列表。 Then, loop through these objects using set_linesytle in order to set the desired linestyle. 然后,使用set_linesytle遍历这些对象,以设置所需的linestyle。

For example: 例如:

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()

在此输入图像描述

The second approach using kde_kws={'linestyle':'--'} works fine with seaborn 8.1. 使用kde_kws={'linestyle':'--'}的第二种方法适用于seaborn 8.1。 Maybe you want to update. 也许你想要更新。

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()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM