【发布时间】: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