Raj Rajeshwari Prasad
Raj Rajeshwari Prasad

Reputation: 334

Seaborn automatically scaling Y axis

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)

enter image description here

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

enter image description here

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

Answers (2)

JohanC
JohanC

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()

example of a countplot

Or using hue:

sns.set_theme("paper")
df = sns.load_dataset('titanic')
ax = sns.countplot(x="pclass", hue="survived", data=df)

countplot with hue

Upvotes: 3

aj7amigo
aj7amigo

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

Related Questions