Python 如何在matplotlib中将一个子地块的图例复制到另一个子地块

Python 如何在matplotlib中将一个子地块的图例复制到另一个子地块,python,matplotlib,Python,Matplotlib,我正在使用Matplotlib绘制一些简单的图形。exmaple如下图所示: x = np.linspace(0, 5, 1000) fig =plt.figure(figsize=(7,7)) ax =plt.subplot(211) ax.plot(x, np.sin(x), '-b', label='Sine') ax.axis('equal') leg1 = ax.legend(loc= (0.8,0.85)); leg2 = ax.legend(loc= (0.8,-0.15));

我正在使用Matplotlib绘制一些简单的图形。exmaple如下图所示:

x = np.linspace(0, 5, 1000)
fig =plt.figure(figsize=(7,7))
ax  =plt.subplot(211)
ax.plot(x, np.sin(x), '-b', label='Sine')
ax.axis('equal')
leg1 = ax.legend(loc= (0.8,0.85));
leg2 = ax.legend(loc= (0.8,-0.15));
leg2.set_zorder(14)
ax.add_artist(leg1)
ax.add_artist(leg2)
ax  =plt.subplot(212)
我想保持上面和下面的子图都有与子图1相同的图例


但是,当我关闭
leg2
以便在第二个子图中显示时,它被画布覆盖。我的问题是,有没有办法将一个子地块的图例复制到另一个子地块

问题在于,第二个图例是第一个子地块的一部分,而第一个子地块完全位于第二个子地块的后面

通常,您会将图例添加到第二个子地块,而不是第一个子地块

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 5, 1000)
fig =plt.figure(figsize=(7,7))
ax  =plt.subplot(211)
ax.plot(x, np.sin(x), '-b', label='Sine')

leg1 = ax.legend(loc= (0.8,0.85))

ax2  =plt.subplot(212)
leg2 = ax2.legend(*ax.get_legend_handles_labels(), loc= (0.8,0.85))


plt.show()