Reputation: 334
I am using Seaborn to plot some data.
The problem I am facing is that Y-axis is automatically getting scaled and not showing actual numbers.
survive_count = sns.barplot(x="Pclass", y='Survived', data=df)
What can be the possible solution to eliminate this?
After few suggestions, I tried
survive_count = sns.countplot(x="Pclass", hue='Survived', data=df)
survive_count.figure.savefig(my_path + '/Class vs Survival Count.png')
But, unfortunately, I landed up on another problem
I am really confused why I am having this inverted image.
To solve this, I tried
plt.gca().invert_yaxis()
and
plt.ylim(reversed(plt.ylim()))
But both solutions did not work.
Upvotes: 0
Views: 1756
Reputation: 80509
You might want to use a countplot. By default, a barplot averages out the values.
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('titanic')
ax = sns.countplot(x="pclass", data=df[df['survived'] == 1])
plt.show()
Or using hue
:
sns.set_theme("paper")
df = sns.load_dataset('titanic')
ax = sns.countplot(x="pclass", hue="survived", data=df)
Upvotes: 3
Reputation: 378
the reason I guess is that seaborns bar plot do the mean value of the category. Please see if any of the code below helps you.
survive_count = sns.barplot(x="Pclass", y='Survived', data=df, estimator=sum)
survive_count = sns.barplot(x="Pclass", y='Survived', data=df, estimator=len)
from numpy import count_nonzero
survive_count = sns.barplot(x="Pclass", y='Survived', data=df, estimator= count_nonzero)
Upvotes: 1