ShikharDua
ShikharDua

Reputation: 10039

Dotted Seaborn distplot

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'

Upvotes: 3

Views: 6591

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339590

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

Upvotes: 5

DavidG
DavidG

Reputation: 25379

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

Upvotes: 2

Related Questions