Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib Legend Guide basic examples

I am trying to understand how to use the legend() better, specifically how to use proxy artists. I find the Legend guide to be severely lacking. This post is somewhat similar to this one.

I am using Python 2.7.5 on Windows 7, matplotlib version 1.2.1. I wrote this code which is essentially a combination of the examples in the legend guide:

import matplotlib.patches as mpatches
import matplotlib.lines as mlines
import matplotlib.pyplot as plt

line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')

blue_line = mlines.Line2D([], [], color='blue', marker='*',
                      markersize=15, label='Blue stars')
red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend([red_patch,blue_line])

plt.show()

http://i.stack.imgur.com/E9jkA.jpg

The legend label colors are not those assigned in the handles, nor are they a patch and a line with stars. I have tried removing and adding the lines, adding and removing the commas after the lines, etc. Are they any better references for using legend()? Any other tips for using proxy artists? I don't understand why the examples give me totally different results...

like image 816
Eric Avatar asked Jun 11 '15 21:06

Eric


People also ask

How does legend work in MatPlotLib?

A legend is an area describing the elements of the graph. In the matplotlib library, there's a function called legend() which is used to Place a legend on the axes. The attribute Loc in legend() is used to specify the location of the legend. Default value of loc is loc=”best” (upper left).

How do I create a multi legend in MatPlotLib?

MatPlotLib with PythonPlace 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

The Legend Guide was rewritten to be compatible with matplotlib version 1.4.0 or newer. Your version of matplotlib (1.2.1) is over two years old. Don't be surprised if everything shown in the docs does not work with such an old version.

If you upgrade your version, then using

plt.legend(handles=[red_patch,blue_line])

(as is shown in the Legend Guide) instead of

plt.legend([red_patch,blue_line])

yields

enter image description here

like image 133
unutbu Avatar answered Nov 02 '22 12:11

unutbu


I'm still using matplotlib 1.2.1 so I'll tell you what works for me. I find that if I pass the line objects to legend(), I also have to pass the labels separately. [This is also consistent with the matplotlib documentation on legened()]. I've modified your example slightly to do this, and it seems to work:

import matplotlib.patches as mpatches
import matplotlib.lines as mlines
import matplotlib.pyplot as plt

line_up, = plt.plot([1,2,3], label='Line 2')
line_down, = plt.plot([3,2,1], label='Line 1')

blue_line = mlines.Line2D([], [], color='blue', marker='*',
                  markersize=15, label='Blue stars')
red_patch = mpatches.Patch(color='red', label='The red data')

lines = [blue_line, red_patch]
labels = [line.get_label() for line in lines]
plt.legend(lines, labels)

plt.show()

code output

like image 23
turnerm Avatar answered Nov 02 '22 11:11

turnerm