Search code examples
pythonseaborndisplot

How to use hist_kws in seaborn displot


I want to plot histogram and kde line in the same plot with different color. I want to set green color for the histogram and blue color for the kde line. I managed to figure out using line_kws to change the kde line color but hist_kws is not working on the displot. I have tried using histplot but I am unable to put different color for the hist and the line.


Solution

  • You can use line_kws={'color': ...} to change the color of the kde line. And directly facecolor=... to change the color of the histogram. The following code has been tested with seaborn 0.11.1 and displot with the default kind (kind='hist') and no hue:

    • sns.displot(..., facecolor=...) changes the color of the histogram faces
    • sns.displot(..., edgecolor=...) changes the color of the histogram edges
    • sns.displot(..., color=...) changes the color of the kde line (when kde=True)
    • sns.displot(..., line_kws={'lw':...}) changes the parameters of the kdeline, except the color

    Here is an example:

    import seaborn as sns
    
    penguins = sns.load_dataset('penguins')
    sns.displot(data=penguins, x="flipper_length_mm", kde=True, col="species", color='red',
                line_kws={'lw': 3}, facecolor='lime', edgecolor='black')
    

    displot, set color for histogram

    Seaborn's forte is the hue parameter, placing multiple distributions together, for which it is very handy that corresponding kde and histogram get the same color. When using hue, the above coloring gets overridden.