【发布时间】:2020-06-06 14:23:11
【问题描述】:
我在 Titanic 数据集上使用 seaborn 散点图和计数图。
这是我绘制散点图的代码。我也尝试编辑图例标签。
ax = seaborn.countplot(x='class', hue='who', data=titanic)
legend_labels, _= ax.get_legend_handles_labels()
pyplot.show();
为了编辑图例标签,我这样做了。在这种情况下,不再有图例标题。如何将此标题从“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();
我使用相同的方法在散点图上编辑图例标签,结果在这里有所不同。它使用“dead”作为图例标题,并使用“survied”作为第一个图例标签。
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();
(1)是否有删除和添加图例标题的参数?
(2) 我在两个不同的图表上使用了相同的代码,图例的结果不同。为什么会这样?
谢谢
【问题讨论】:
-
图例标题通过
ax.get_legend().set_title("bla")设置。在 seaborn 散点图的情况下,appears 的标题只是另一个没有相应句柄的标签。在这种情况下,请参阅stackoverflow.com/a/51579663/4124317
标签: python matplotlib data-visualization seaborn legend