#21 Control rug and density on seaborn histogram

By default, the displot function of seaborn plots an histogram with a density curve (see graph #20). You can easily remove the density using the option kde=”False”. You can also control the presence of rugs using rug=”True”. You can custom rug and density as proposed below:

# Import library and dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Hist only
sns.distplot( a=df["sepal_length"], hist=True, kde=False, rug=False )

# Hist + Rug + kernel density
sns.distplot( a=df["sepal_length"], hist=True, kde=True, rug=True )

# To change parameters of rug
sns.distplot( a=df["sepal_length"], rug=True,
rug_kws={"color": "r", "alpha":0.3, "linewidth": 2, "height":0.2 }

# To change parameters of density distribution
sns.distplot( a=df["sepal_length"], kde=True,
kde_kws={"color": "g", "alpha":0.3, "linewidth": 5, "shade":True }

Leave a Reply

Your email address will not be published.