【问题标题】:How to add legends to sns subplots?如何将图例添加到 sns 子图?
【发布时间】:2021-08-17 21:22:13
【问题描述】:

期望的结果:

所以我试图在子图中添加图例, 我试图将 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()

【问题讨论】:

标签: python pandas matplotlib seaborn subplot


【解决方案1】:

我知道现在该做什么了:

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

【讨论】:

    猜你喜欢
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    相关资源
    最近更新 更多