Search code examples
pythonseaborncolorbarjointplotkdeplot

How to add a colorbar to kdeplot and jointplot


When generating bivariate plots like hexbin, pandas generates a legend explaining frequency value for each color shade:

pokemon.plot.hexbin(x='HP', y='Attack', gridsize=30)

Pandas hexbin plot

I cannot find a similar way to generate such a legend for jointplot and kdeplot in seaborn:

sns.jointplot(data=pokemon, x='HP', y='Attack', kind='hex')

Seaborn hexbin plot

sns.kdeplot(pokemon['HP'], pokemon['Attack'], shade=True)

Seaborn contour lines plot

How can I do it?


Solution

  • for a kdeplot, simply pass cbar=True

    cbar : bool, optional

    If True and drawing a bivariate KDE plot, add a colorbar.
    
    import seaborn as sns
    
    g = sns.jointplot(data=sns.load_dataset("penguins"), x="bill_length_mm", y="bill_depth_mm",
                      kind="kde", cbar=True, fill=True, height=8)
    

    enter image description here