Seaborn自动缩放Y轴

2024-10-01 22:41:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Seaborn绘制一些数据

我面临的问题是Y轴会自动缩放,而不会显示实际数字

survive_count = sns.barplot(x="Pclass", y='Survived', data=df)

enter image description here

消除这种情况的可能解决方案是什么

在提出了几点建议后,我试了一下

survive_count = sns.countplot(x="Pclass", hue='Survived', data=df)
survive_count.figure.savefig(my_path + '/Class vs Survival Count.png')

但不幸的是,我遇到了另一个问题

enter image description here

我真的很困惑为什么我会有这个倒转的图像

为了解决这个问题,我试着

plt.gca().invert_yaxis()

plt.ylim(reversed(plt.ylim()))

但这两种解决方案都不起作用


Tags: 数据dfdatacount绘制plt数字seaborn
2条回答

您可能需要使用countplot。默认情况下,条形图对值进行平均

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

或使用hue

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

countplot with hue

我猜原因是seaborns bar plot计算了类别的平均值。 请查看下面的代码是否对您有帮助

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)

相关问题 更多 >

    热门问题