色相の凡例を変更するにはどうすればよいですか?

Aug 23 2020

男性と女性をバイナリで表すデータセットがあります。男性は0として表され、女性は1として表されます。私がやりたいのは、プロットの凡例で0を男性に、1を女性に変更することです。この投稿をフォローしようとしましたが、うまくいきませんでした。

次のようなエラーメッセージが表示されます。

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

回答

1 Ynjxsjmh Aug 23 2020 at 15:00

私はあなたのデータを持っていないので、私は自分でいくつかを生成します:

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']交換する0maleして1female

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'

gによって返されるのsns.scatterplotはクラスmatplotlib.axes.Axesです。そこからlengendオブジェクトを取得するには、。ではなくax.get_legend()orを使用する必要があります。オフィシャルレジェンドガイドのドキュメントに従うことができます。ax.legend()ax._legend

legend = g.legend()

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