期望的结果:

因此,我试图将图例添加到子图中,我试图将ax.legend()添加到代码中,但它引发了"No handles with labels to put 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()发布于 2021-05-30 16:14:11
我知道现在该怎么做了:
ax[0].legend(['Survived', 'Not Survived'])
ax[1].legend(['Survived', 'Not Survived'])

https://stackoverflow.com/questions/67759005
复制相似问题