Seaborn 条形图图例标签失去颜色 [英] Seaborn barplot legend labels lose color

查看:61
本文介绍了Seaborn 条形图图例标签失去颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个深奥的箱线图,当我尝试使用plt.legend("Strings")更改标签名称时,它会丢失标签的颜色.我需要在保持颜色编码的同时更改标签,但在搜索答案后我不知道该怎么做.

色调图例 1-4 对应于从 1 = 对政治非常感兴趣到 4 = 完全不感兴趣.我想将图例色相标签从1-4更改为对政治的兴趣.

我的代码是:

将pandas导入为pd将numpy导入为np将 seaborn 作为 sns 导入导入matplotlib.pyplot作为plt

我不知道如何以任何更简单的方式创建数据框,所以我做到了

a1 = {'读报纸':0,'对政治的兴趣':1}a2 = {'读报纸':0,'对政治的兴趣':2}a3 = {阅读报纸":0,对政治的兴趣":3}a4 = {'读报纸':0,'对政治的兴趣':4}b1 = {'读报纸':1,'对政治的兴趣':1}b2 = {'读报纸':1,'对政治的兴趣':2}b3 = {'读报纸':1,'对政治的兴趣':3}b4 = {'读报纸':1,'对政治的兴趣':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)数据 = pd.concat([df1,df2,df3,df4,b1,b2,b3,b4])

实际绘图会产生错误

  plt.figure(figsize =(10,8))g = sns.barplot(data = data,x ='Read Newspapers',estimator = len,y ='Politics in Politics',hue ='Politics in Politics')plt.ylabel(样本大小")斧= plt.subplot()ax = ax.set_xticklabels([否",是"])#plt.legend([非常感兴趣",有点感兴趣",只有一点感兴趣",一点都不感兴趣"])#plt.savefig('报纸政策')

我尝试使用 plt.legend ,但是图例标签在执行此操作时会丢失颜色,因此它变成没有颜色关联的字符串,甚至比以前更糟.

我现在已经编辑了整个脚本.

手动设置标签.

您可以通过 ax.get_legend_handles_labels()获取图例的句柄和标签,并使用它们从列表中创建带有标签的新图例.

 将matplotlib.pyplot导入为plt将 seaborn 作为 sns 导入将熊猫作为pd导入df = pd.DataFrame({"reads":["Yes"] * 250 + ["No"] * 150,兴趣":[4,2,2,2,2,3,3,1,1,1]*40})标签=[非常感兴趣",有点感兴趣",只是有点兴趣",完全不感兴趣"]plt.figure(figsize=(10,8))ax = sns.barplot(data=df, x='reads', estimator=len,y='interest',hue='interest' )ax.set_ylabel("样本大小")h,l = ax.get_legend_handles_labels()ax.legend(h,labels,title =对政治感兴趣")plt.show()

I have a seaborn boxplot which when I try to use plt.legend("Strings") to change name of labels it loses the colors of the labels. I need to change labels while maintaining the color coding, but I do not know how to do this after searching for an answer.

The Hues legend 1-4 corresponds from 1 = Very interested in politics to 4 = not at all interested. I want to change the legend hue labels from 1-4 to how interested they are in politics.

My code is:

Packages

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

I didnt know how to create dataframe in any simpler way so i did this

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])

Actual plotting that produces error

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')

I tried using plt.legend but the legend labels lose their color when I do this so it becomes strings with no color association, making it even worse than before.

I have now editted in the entirety of my script.

https://github.com/HenrikMorpheus/Newspaper-reading-survey/blob/master/politicalinterest.ipynb It loads with an error for some reason i dont know, but you should be able to open the notebook in jupyter.

解决方案

Use dedicated dataframe column

An option is to create a new column in the dataframe with the respective labels in, and use this column as input for the hue, such that the desired labels are automatically created.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd


df = pd.DataFrame({"reads" : ["Yes"] * 250 + ["No"]*150,
                  "interest" : [4,2,2,2,2,3,3,1,1,1]*40})

labels=["very interested","somewhat interested", 
        "only a little interested", "not at all interested"]
# Create new dataframe column with the labels instead of numbers
df["Interested in politics"] = df["interest"].map(dict(zip(range(1,5), labels)))

plt.figure(figsize=(10,8))
# Use newly created dataframe column as hue
ax = sns.barplot(data=df, x='reads', estimator=len,y='interest', 
                 hue='Interested in politics', hue_order=labels)
ax.set_ylabel("Sample Size")

plt.show()

Setting the labels manually.

You may obtain the handles and labels for the legend via ax.get_legend_handles_labels() and use them to create a new legend with the labels from the list.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = pd.DataFrame({"reads" : ["Yes"] * 250 + ["No"]*150,
                  "interest" : [4,2,2,2,2,3,3,1,1,1]*40})

labels=["very interested","somewhat interested", 
        "only a little interested", "not at all interested"]

plt.figure(figsize=(10,8))
ax = sns.barplot(data=df, x='reads', estimator=len,y='interest', hue='interest' )
ax.set_ylabel("Sample Size")

h, l = ax.get_legend_handles_labels()
ax.legend(h, labels, title="Interested in politics")
plt.show()

这篇关于Seaborn 条形图图例标签失去颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆