Griff
Griff

Reputation: 2124

plot nothing but the legend in matplotlib subplot

I have a bunch of subplots and one I want to contain a legend (there are the same number of handles and labels in each of the other subplots so I just want a master legend for all of them).

|plot|gap1|plot|
|plot|plot|plot|
|plot|gap2|plot|

Now where the gap1, I want to put my master legend. I plotted the same in the first subplot.

line1 = ax2.plot(x1,y2,'--',color='b',linewidth=2)
line2 = ax2.plot(x1,y2,'-',color='r')
ax2.legend((line1,line2),('line1','line2'),numpoints=1,loc='center')
l = line1[0]
l.remove()
l = line2[0]
l.remove()
ax2.set_axis_off()

But this doesn't seem to work.

Now I just want to put the legend in the center for the ax2 subplot (in gap1) and remove everything (axis, lines, ticks etc.) except the legend which is in the center. Thanks.

Upvotes: 2

Views: 749

Answers (1)

tacaswell
tacaswell

Reputation: 87556

fig, axes_lst = plt.subplots(2, 1)

ln_1, ln_2 = axes_lst[0].plot(range(5), range(5), range(5), np.arange(5) ** 2)
axes_lst[1].legend((ln_1, ln_2), ('lin', 'quad'), loc='center')
plt.draw()

The handles used in the call to legend don't have to be on the axes that you calling legend on.

enter image description here

Upvotes: 1

Related Questions