Cookbook/Matplotlib/Legend

Legends for overlaid lines and markers

If you have a lot of points to plot, you may want to set a marker only once every n points, e.g.

plot(x, y, '-r')
plot(x[::20], y[::20], 'ro')

Then the automatic legend sees this as two different plots. One approach is to create an extra line object that is not plotted anywhere but used only for the legend:

from matplotlib.lines import Line2D
line = Line2D(range(10), range(10), linestyle='-', marker='o')
legend((line,), (label,))

Another possibility is to modify the line object within the legend:

line = plot(x, y, '-r')
markers = plot(x[::20], y[::20], 'ro')
lgd = legend([line], ['data'], numpoints=3)
lgd.get_lines()[0].set_marker('o')
draw()

Legend outside plot

There is no nice easy way to add a legend outside (to the right of) your plot, but if you set the axes right in the first place, it works OK:

figure()
axes([0.1,0.1,0.71,0.8])
plot([0,1],[0,1],label="line 1")
hold(1)
plot([0,1],[1,0.5],label="line 2")
legend(loc=(1.03,0.2))
show()

Removing a legend from a plot

One can simply set the legend_ attribute of the axes to None and redraw:

ax = gca()
ax.legend_ = None
draw()

If you find yourself doing this often use a function such as

   1 def remove_legend(ax=None):
   2     """Remove legend for ax or the current axes."""
   3 
   4     from pylab import gca, draw
   5     if ax is None:
   6         ax = gca()
   7     ax.legend_ = None
   8     draw()

(Source: Re: How to delete legend with matplotlib OO from a graph?)

Changing the font size of legend text

Note that to set the default font size, one can change the legend.size property in the matplotlib rc parameters file.

To change the font size for just one plot, use the matplotlib.font_manager.FontProperties argument, prop, for the legend creation.

x = range(10)
y = range(10)
handles = plot(x, y)
legend(handles, ["label1"], prop={"size":12})

Cookbook/Matplotlib/Legend (last edited 2011-11-09 17:38:20 by TimSwast)