Reputation: 91
I have to plot two distplots from a dataframe based on a binary variable:
df_train \
.groupby("Binary")["Continuous"] \
.apply(sb.distplot, hist=False)
plt.show()
If I pass the argument label= in apply, the same label will be applied to both the plots. How can I tell him to show the label based on the value of the binary feature?
Upvotes: 0
Views: 291
Reputation: 80459
Instead of using apply
, you could loop through the created group. (Instead of distplot
without histogram, kdeplot
can be called directly, which makes it easier to provide additional parameters.)
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
df_train = pd.DataFrame({"Binary": np.repeat(['yes', 'no'], 1000),
"Continuous": np.random.uniform(-1, 1, 2000).cumsum()})
groups = df_train.groupby("Binary")["Continuous"]
for label, group in groups:
sns.kdeplot(group, label=f"Binary: {label}", shade=True)
plt.show()
Upvotes: 1