3

I have a seaborn displot which looks like this enter image description here

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'
ShikharDua
  • 9,411
  • 1
  • 26
  • 22

2 Answers2

5

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

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
2

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

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82