1

I use pairplot with density plot on diagonal positions, but the line style for different groups are the same. Is there a way to use different line styles for different groups?

Any help is appreciated!

import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")

g = sns.pairplot(iris, kind="scatter", 
                 hue = 'species', 
                 diag_kind='auto',
                 diag_kws={'bw_adjust':.03, 'linestyle':['solid','dotted', 'dashed', 'dashdot']},
                 # this does not work with error: Unrecognized linestyle: ['solid', 'dotted', 'dashed', 'dashdot'], same for: {'solid','dotted', 'dashed', 'dashdot'}
                 #diag_kws={'bw':.03, 'linestyle':'dotted'} # this works but has identical line style for species.
                )
plt.show()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Xin Niu
  • 533
  • 1
  • 5
  • 15

1 Answers1

3
  • 'linestyle':' dotted' is the expected behavior g = sns.pairplot(iris, kind="scatter", hue='species', diag_kind='kde', diag_kws={'bw_adjust':.1, 'ls': 'dotted'}). It is not possible to set the ls/linestyle parameter with multiple values.
  • As noted in a comment by @JohanC, the key is properly extracting the diagonal axes objects with g.diag_axes not axes[[0, 5, 10, 15]].
    • Once this has been corrected, this solution works without issue.
import seaborn as sns

# load the data
iris = sns.load_dataset("iris")

g = sns.pairplot(iris, diag_kind='kde', hue='species', diag_kws={'fill': False})

diag = g.diag_axes

lss = ['dotted', 'dashed', 'dashdot']

for ax in diag:
   
    for line, ls in zip(ax.lines, lss):
        line.set_linestyle(ls)

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158