首页 > 解决方案 > 在子图中复制图例

问题描述

我试图将图例放在 2 个子图之外,fig.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)但图例仍在框中。我如何将这两个图例组合在框中并将其放在外面?

fig, (ax1, ax2) = plt.subplots(1, 2,sharey=True)
sns.lineplot(data=PopulationCensus2020.iloc[1:15,1:7],ax=ax1)


sns.lineplot(data=PopulationCensus2010.iloc[1:15,1:7],ax=ax2)
fig.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

for ax in fig.axes:
    plt.sca(ax)
    plt.xticks(rotation=45)

ax1.title.set_text('2020')
ax2.title.set_text('2010')
ax1.set_ylabel('Number of couples')

在此处输入图像描述

标签: pythonmatplotlibseabornlegend

解决方案


推荐阅读