【问题标题】:How to alter the hue legend?如何更改色调图例?
【发布时间】:2020-08-23 06:59:05
【问题描述】:

我有一个以二进制表示男性和女性的数据集。男性表示为 0,而女性表示为 1。我希望做的是在情节图例中将 0 更改为男性,将 1 更改为女性。我尝试关注this post,但没有成功。

它给了我一个如下所示的错误消息:

AttributeError                            Traceback (most recent call last)
<ipython-input-11-b3c99d4311ab> in <module>
     23 # plot the legend
     24 plt.legend()
---> 25 legend = g._legend
     26 new_labels = ['Female', 'Male']
     27 for t, l in zip(legend.texts, new_labels): t.set_text(l)

AttributeError: 'AxesSubplot' object has no attribute '_legend'

这是我当前代码的样子:

## store them in different variable names
X = salary['years']
y = salary['salary']
g = salary['gender']

# prepare the scatterplot
sns.set()
plt.figure(figsize=(10,10))
g = sns.scatterplot(x=salary.years, y=salary.salary, data=salary, hue='gender')

# equations of the models
model1 = 50 + 2.776962335386217*X
model2 = 60.019802 + 2.214645*X
model3_male = 60.014922 + 2.179305*X + 1.040140*1
model3_female = 60.014922 + 2.179305*X + 1.040140*0

# plot the scatterplots
plt.plot(X, model1, color='r', label='Model 1')
plt.plot(X, model2, color='g', label='Model 2')
plt.plot(X, model3_male, color='b', label='Model 3(Male)')
plt.plot(X, model3_female, color='y', label='Model 3(Female)')

# plot the legend
plt.legend()
legend = g._legend
new_labels = ['Female', 'Male']
for t, l in zip(legend.texts, new_labels): t.set_text(l)

# set the title
plt.title('Scatterplot of salary and model fits')
plt.show()

【问题讨论】:

    标签: python-3.x matplotlib seaborn


    【解决方案1】:

    我没有你的数据,所以我自己生成一些:

    gender salary years
    male 40000 1
    male 32000 2
    male 45000 3
    male 54000 4
    female 72000 5
    female 62000 6
    female 92000 7
    female 55000 8
    female 35000 9
    female 48000 10
    
    import seaborn as sns
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    
    salary = pd.read_csv("1.csv", delim_whitespace=True)
    
    print(salary)
    
    X = salary['years']
    y = salary['salary']
    g = salary['gender']
    
    
    # prepare the scatterplot
    sns.set()
    plt.figure(figsize=(10,10))
    g = sns.scatterplot(x=salary.years, y=salary.salary, data=salary, hue='gender')
    
    # equations of the models
    model1 = 50 + 2.776962335386217*X
    model2 = 60.019802 + 2.214645*X
    model3_male = 60.014922 + 2.179305*X + 1.040140*1
    model3_female = 60.014922 + 2.179305*X + 1.040140*0
    
    # plot the scatterplots
    plt.plot(X, model1, color='r', label='Model 1')
    plt.plot(X, model2, color='g', label='Model 2')
    plt.plot(X, model3_male, color='b', label='Model 3(Male)')
    plt.plot(X, model3_female, color='y', label='Model 3(Female)')
    
    # plot the legend
    plt.legend()
    
    # set the title
    plt.title('Scatterplot of salary and model fits')
    
    plt.show()
    

    它工作正常。所以我猜你gender 列中的值是01。在这种情况下,您可以在g = salary['gender'] 之前执行以下操作,将0 替换为male 并将1 替换为female

    salary['gender'] = salary['gender'].map({1: 'female', 0: 'male'})
    

    回到你的错误:

    ---> 25 legend = g._legend
         26 new_labels = ['Female', 'Male']
         27 for t, l in zip(legend.texts, new_labels): t.set_text(l)
    
    AttributeError: 'AxesSubplot' object has no attribute '_legend'
    

    sns.scatterplot 返回的g 是类matplotlib.axes.Axes。要从中获取lengend 对象,您需要使用ax.get_legend()ax.legend() 而不是ax._legend。可以关注官方Legend guide documentation

    legend = g.legend()
    
    new_labels = ['Female', 'Male']
    for t, l in zip(legend.texts[-2:], new_labels): t.set_text(l)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      相关资源
      最近更新 更多