Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a legend outside of multiple subplots with matplotlib

I am making a few figures where each one has a different amount of subplots in it. I am trying to add a legend in the bottom right corner but am having some trouble. I tried adding a new subplot in the bottom right and adding a legend only to it but then had an empty subplot behind the legend. This is where I'm standing now but want the legend in the bottom right corner regardless of where the last subplot is.

fig = plt.figure()
matplotlib.rc('xtick', labelsize=8) 
matplotlib.rc('ytick', labelsize=8)

for line in a[1:]:

        ax = fig.add_subplot(subcol,subrow,counter)
        ax.plot(x,line[3:7],marker='o', color='r', label = 'oral')
        ax.plot(x,line[7:],marker='o', color='b',label = 'physa')
        ax.set_title(line[1],fontsize = 10)
        counter+=1

ax.legend(bbox_to_anchor=(2, 0),loc = 'lower right')
plt.subplots_adjust(left=0.07, right=0.93, wspace=0.25, hspace=0.35)
plt.suptitle('Kegg hedgehog',size=16)
plt.show()
like image 307
Amos Avatar asked Jun 02 '14 13:06

Amos


People also ask

How do I add a legend to all subplots?

Create a figure and a set of subplots, using the subplots() method, considering 3 subplots. Plot the curve on all the subplots(3), with different labels, colors. To place the legend for each curve or subplot adding label. To activate label for each curve, use the legend() method.

How do you add a legend to the outside of a chart?

To place the legend outside of the axes bounding box, one may specify a tuple (x0, y0) of axes coordinates of the lower left corner of the legend. places the legend outside the axes, such that the upper left corner of the legend is at position (1.04, 1) in axes coordinates.

How do I control the location of a legend in Matplotlib?

To change the position of a legend in Matplotlib, you can use the plt. legend() function. The default location is “best” – which is where Matplotlib automatically finds a location for the legend based on where it avoids covering any data points.

How do I add another legend in Matplotlib?

Place the first legend at the upper-right location. Add artist, i.e., first legend on the current axis. Place the second legend on the current axis at the lower-right location. To display the figure, use show() method.


2 Answers

A better solution is

fig.legend(handles, labels, loc='', ...)

This adds the legend to the figure instead of the subplot.

Adapted to your example, it would be something like

fig = plt.figure()
matplotlib.rc('xtick', labelsize=8) 
matplotlib.rc('ytick', labelsize=8)

handles = []
for line in a[1:]:

        ax = fig.add_subplot(subcol,subrow,counter)
        l1 = ax.plot(x,line[3:7],marker='o', color='r', label = 'oral')
        l2 = ax.plot(x,line[7:],marker='o', color='b',label = 'physa')
        if not handles:
           handles = [l1, l2]
        ax.set_title(line[1],fontsize = 10)
        counter+=1

fig.legend(handles, ['oral', 'physa'], bbox_to_anchor=(2, 0),loc = 'lower right')
plt.subplots_adjust(left=0.07, right=0.93, wspace=0.25, hspace=0.35)
plt.suptitle('Kegg hedgehog',size=16)
plt.show()
like image 64
has2k1 Avatar answered Nov 17 '22 18:11

has2k1


I ended up adding an extra subplot which I removed the frame and axis of, then plotted nothing to it and added a legend

lastSubplot = plt.subplot(subcol,subrow,subcol*subrow)
lastSubplot.set_frame_on(False)
lastSubplot.get_xaxis().set_visible(False)
lastSubplot.get_yaxis().set_visible(False)
plt.plot(0, 0, marker='o', color='r', label = 'line1')
plt.plot(0, 0, marker='o', color='b', label = 'line2')
lastSubplot.legend(loc = 'lower right')

This left me with only the legend in the bottom right corner regardless of how many subplots I actually had.

like image 23
Amos Avatar answered Nov 17 '22 16:11

Amos