【问题标题】:Edit legend title of Seaborn scatterplot and countplot编辑 Seaborn 散点图和计数图的图例标题
【发布时间】: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


【解决方案1】:

尝试使用

ax.legend(legend_labels, ['man1','woman1','child1'], 
          bbox_to_anchor=(1,1), 
          title='whatever title you want to use')

【讨论】:

  • 谢谢!我所要做的就是使用标题
  • 真的很有帮助!拯救了我的一天!
猜你喜欢
  • 2021-10-28
  • 2021-01-29
  • 2019-07-08
  • 1970-01-01
  • 2019-04-15
  • 2017-12-25
  • 2020-11-07
  • 2020-12-04
  • 2014-09-08
相关资源
最近更新 更多