I am trying to plot the output from the predict of a ML model, there are the classes 1,0 for the Target, and the Score. Due the dataset is not balanced, there are few 1’s.
When I plot a simple displot with the Target in the hue parameter, the plot is useless for describing the 1’s
JavaScript
x
5
1
sns.set_theme()
2
sns.set_palette(sns.color_palette('rocket', 3))
3
sns.displot(df, x='Score', hue='Target', bins=30, linewidth=0, height=5, kde=True, aspect=1.6)
4
plt.show()
5
I want to change the scale for the 1’s in the same plot, with a second y-scale in the right with twinx.
I have tried the following codes that may solve the problem with 2 plots, but I need only one plot. I couldn’t use twinx.
JavaScript
1
4
1
g = sns.displot(df, x='Score', col='Target', bins=30, linewidth=0, height=5, kde=True, aspect=1.6, facet_kws={'sharey': False, 'sharex': False})
2
g.axes[0,1].set_ylim(0,400)
3
plt.show()
4
JavaScript
1
3
1
g = sns.FacetGrid(df, hue='Target')
2
g = g.map(sns.displot, 'Score', bins=30, linewidth=0, height=3, kde=True, aspect=1.6)
3
A reproducible example could be with the titanic dataset:
JavaScript
1
3
1
df_ = sns.load_dataset('titanic')
2
sns.displot(df_, x='fare', hue='survived', bins=30, linewidth=0, height=5, kde=True, aspect=1.6)
3
JavaScript
1
4
1
g = sns.displot(df_, x='fare', col='survived', bins=30, linewidth=0, height=5, kde=True, aspect=1.6, facet_kws={'sharey': False, 'sharex': False})
2
g.axes[0,1].set_ylim(0,150)
3
plt.show()
4
Advertisement
Answer
I am not sure but are you looking for this.
JavaScript
1
8
1
import seaborn as sns
2
import matplotlib.pyplot as plt
3
sns.set()
4
df_ = sns.load_dataset('titanic')
5
sns.histplot(df_[df_['survived']==1]['fare'], bins=30, linewidth=0, kde=True, color='red')
6
ax2 = plt.twinx()
7
sns.histplot(df_[df_['survived']==0]['fare'], bins=30, linewidth=0, kde=True, ax=ax2, color='blue')
8