为了账号安全,请及时绑定邮箱和手机立即绑定

Seaborn 条形图图例标签失去颜色

Seaborn 条形图图例标签失去颜色

缥缈止盈 2021-09-14 17:39:24
我有一个 seaborn boxplot,当我尝试使用 plt.legend("Strings") 更改标签名称时,它会丢失标签的颜色。我需要在保持颜色编码的同时更改标签,但在搜索答案后我不知道如何执行此操作。色调图例 1-4 对应于 1 = 对政治非常感兴趣到 4 = 完全不感兴趣。我想将图例色调标签从 1-4 更改为他们对政治的兴趣程度。我的代码是:套餐import pandas as pdimport numpy as npimport seaborn as snsimport matplotlib.pyplot as plt我不知道如何以更简单的方式创建数据框,所以我这样做了a1 = {'Reads Newspapers': 0, 'Interest in Politics': 1}a2 = {'Reads Newspapers': 0, 'Interest in Politics': 2}a3 = {'Reads Newspapers': 0, 'Interest in Politics': 3}a4 = {'Reads Newspapers': 0, 'Interest in Politics': 4}b1 = {'Reads Newspapers': 1, 'Interest in Politics': 1}b2 = {'Reads Newspapers': 1, 'Interest in Politics': 2}b3 = {'Reads Newspapers': 1, 'Interest in Politics': 3}b4 = {'Reads Newspapers': 1, 'Interest in Politics': 4}df1 = pd.DataFrame(data=a1, index=range(1))df1 = pd.concat([df1]*23)df2 = pd.DataFrame(data=a2, index=range(1))df2 = pd.concat([df2]*98)df3 = pd.DataFrame(data=a3, index=range(1))df3 = pd.concat([df3]*99)df4 = pd.DataFrame(data=a4, index=range(1))df4 = pd.concat([df4]*18)b1 = pd.DataFrame(data=b1, index=range(1))b1 = pd.concat([b1]*468)b2 = pd.DataFrame(data=b2, index=range(1))b2 = pd.concat([b2]*899)b3 = pd.DataFrame(data=b3, index=range(1))b3 = pd.concat([b3]*413)b4 = pd.DataFrame(data=b4, index=range(1))b4 = pd.concat([b4]*46)data = pd.concat([df1,df2,df3,df4,b1,b2,b3,b4])产生错误的实际绘图plt.figure(figsize=(10,8))g = sns.barplot(data=data, x='Reads Newspapers', estimator=len,y='Interest in Politics', hue='Interest in Politics' )plt.ylabel("Sample Size")ax = plt.subplot()ax = ax.set_xticklabels(["No","Yes"])#plt.legend(["very interested","somewhat interested", "only a little interested", "not at all interested "],)#plt.savefig('Newspaper policy')我尝试使用,plt.legend但是当我这样做时,图例标签会失去颜色,因此它变成了没有颜色关联的字符串,使其比以前更糟。我现在已经编辑了我的整个脚本。https://github.com/HenrikMorpheus/Newspaper-reading-survey/blob/master/politicalinterest.ipynb 由于某种我不知道的原因加载了错误,但您应该能够在 jupyter 中打开笔记本。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 321 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信