Change color of seaborn distribution line

12,424

For changing the color of the fitted curve, you need to set fit_kws argument. But fit_kws does not support shading. You can still shade the area below the fitted curve by a few extra lines of code as shown below but that I think is an answer to another question that you have posted.

import numpy as np
import seaborn as sns
import scipy.stats as stats
import matplotlib.pyplot as plt

sns.set()
np.random.seed(0)
x = np.random.randn(100)
y = np.random.normal(loc=6.0, scale=1, size=(50,))

ax = sns.distplot(x, fit_kws={"color":"red"}, kde=False,
        fit=stats.gamma, hist=None, label="label 1");
ax = sns.distplot(y, fit_kws={"color":"blue"}, kde=False,
        fit=stats.gamma, hist=None, label="label 2");

plt.show(block=False)

The result of the code is show below: enter image description here

Share:
12,424
JAG2024
Author by

JAG2024

Just a graduate student eternally grateful for SO.

Updated on June 05, 2022

Comments

  • JAG2024
    JAG2024 almost 2 years

    I want to specify the color of a line of fit within the seaborn package for an array of x and y data. Instead all I can figure out is how to change the color and shading for the kernel density function. How can I change the color for a gaussian fit? I.e. the lines below should be red and blue. It would also be great to shade in the function like the "shade":True argument.

    import seaborn as sns
    sns.distplot(x,kde_kws={"shade":True}, kde=False, fit=stats.gamma, hist=None, color="red", label="label 1");
    sns.distplot(y,kde_kws={"shade":True}, kde=False, fit=stats.gamma, hist=None, color="blue", label="label 2");
    

    enter image description here

  • JAG2024
    JAG2024 over 6 years
    Thanks for this. I had a tough time finding the arguments for fit_kws. Bummer that's it's complicated to shade.
  • Admin
    Admin over 6 years
    Yes. It is a bit complicated to shade.