简体   繁体   中英

Dotted Seaborn distplot

I have a seaborn displot which looks like this 在此输入图像描述

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

#### 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 . Then, loop through these objects using set_linesytle in order to set the desired 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. 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()

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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