簡體   English   中英

如何將圖例添加到 sns 子圖?

[英]How to add legends to sns subplots?

期望的結果:

錯誤結果

所以我試圖向子圖添加圖例,我試圖將 ax[0].legend() 添加到代碼中,但它會引發“沒有找到帶有標簽的句柄放入圖例”。

fig, ax = plt.subplots(1, 2, figsize=(15, 6))
ax = ax.flatten()
sns.histplot(train[train['Survived'] == 0]['Age'], ax=ax[0])
sns.histplot(train[train['Survived'] == 1]['Age'], color='red', alpha=0.4, ax=ax[0])
sns.histplot(train[train['Survived'] == 0]['Fare'], ax=ax[1])
sns.histplot(train[train['Survived'] == 1]['Fare'], color='red', alpha=0.4, ax=ax[1])
plt.show()

我知道現在該怎么做:

ax[0].legend(['Survived', 'Not Survived'])
ax[1].legend(['Survived', 'Not Survived'])

回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM