Home > front end >  How to add legend of DataFrame columns to kdeplot facetgrid?
How to add legend of DataFrame columns to kdeplot facetgrid?

Time:05-20

I am using a DataFrame like below:

df = pd.DataFrame({'a': [20, 30, 50, 55], 'b': [100, 50, 20, 15], 'c':[15, 20, 400, 10]})

And I tried this:

(sns
.FacetGrid(data = df,
            height=10,
            xlim=(0, 10),
            legend_out= True
).add_legend()
.map(sns.kdeplot, data = df, shade = True)
)

And it produced this: facetgrid with kde plots

CodePudding user response:

This works for me

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

d = {"aa": [20, 30, 50, 55], "bb": [100, 50, 20, 15], "cc": [15, 20, 400, 10]}
df = pd.DataFrame(data=d)

g = sns.FacetGrid(data=df, height=10, xlim=(0, 10))
g.map_dataframe(sns.kdeplot, data=df, shade=True)

legend_names = dict(d.keys())
plt.legend(legend_names)
plt.show(block=True)
  • Related