编辑Seaborn散点图和countplot的图例标题

2024-06-17 10:30:14 发布

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

我正在泰坦尼克号数据集上使用seaborn散点图和countplot
这是我绘制散点图的代码。我还尝试编辑图例标签

ax = seaborn.countplot(x='class', hue='who', data=titanic)
legend_labels, _= ax.get_legend_handles_labels()
pyplot.show();

output

为了编辑图例标签,我这样做了。在本例中,不再有图例标题。如何将此标题从“who”重命名为“who1”

ax = seaborn.countplot(x='class', hue='who', data=titanic)
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(legend_labels, ['man1','woman1','child1'], bbox_to_anchor=(1,1))
pyplot.show();

output2

我用同样的方法编辑散点图上的图例标签,结果在这里是不同的。它使用“死亡”作为图例标题,使用“生存”作为第一个图例标签

ax = seaborn.scatterplot(x='age', y='fare', data=titanic, hue = 'survived')
legend_labels, _= ax.get_legend_handles_labels()
ax.legend(legend_labels, ['dead', 'survived'],bbox_to_anchor=(1.26,1))
pyplot.show();

enter image description here

(1)是否有删除和添加图例标题的参数

(2)我在两个不同的图形上使用了相同的代码,图例的结果不同。为什么

多谢各位


Tags: 编辑标题datagetlabels标签seabornax