You can capture the descriptors and labels of the legend from the first subheading using ax1.get_legend_handles_labels()
, and then use them when creating the legend to the second subheading.
From docs :
get_legend_handles_labels(legend_handler_map=None)
Returns descriptors and labels for a legend
ax.legend()
equivalent to:
h, l = ax.get_legend_handles_labels() ax.legend(h, l)
import numpy as np from matplotlib import gridspec, pyplot as plt x = np.linspace(0,100) y = np.sin(x) fig = plt.figure() gs = gridspec.GridSpec( 100,100 ) ax1 = fig.add_subplot(gs[ :50, : ]) ax2 = fig.add_subplot(gs[ 55:, : ]) ax1.plot( x, y, label=r'sine' ) h,l=ax1.get_legend_handles_labels()

source share