Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib Legends not working

Ever since upgrading matplotlib I get the following error whenever trying to create a legend:

/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30810>] Use proxy artist instead.  http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist    warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),)) /usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x3a30990>] Use proxy artist instead.  http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist    warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),)) 

This even occurs with a trivial script like this:

import matplotlib.pyplot as plt  a = [1,2,3] b = [4,5,6] c = [7,8,9]  plot1 = plt.plot(a,b) plot2 = plt.plot(a,c)  plt.legend([plot1,plot2],["plot 1", "plot 2"]) plt.show() 

I've found the link that the error points me towards pretty useless in diagnosing the source of the error.

like image 230
Mike Vella Avatar asked Aug 16 '12 08:08

Mike Vella


People also ask

How do I fix the legend position 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.

Can I put legend outside plot Matplotlib?

Matplotlib set legend outside plot In Matplotlib, to set a legend outside of a plot you have to use the legend() method and pass the bbox_to_anchor attribute to it. We use the bbox_to_anchor=(x,y) attribute. Here x and y specify the coordinates of the legend.


2 Answers

You should add commas:

plot1, = plt.plot(a,b) plot2, = plt.plot(a,c) 

The reason you need the commas is because plt.plot() returns a tuple of line objects, no matter how many are actually created from the command. Without the comma, "plot1" and "plot2" are tuples instead of line objects, making the later call to plt.legend() fail.

The comma implicitly unpacks the results so that instead of a tuple, "plot1" and "plot2" automatically become the first objects within the tuple, i.e. the line objects you actually want.

http://matplotlib.sourceforge.net/users/legend_guide.html#adjusting-the-order-of-legend-items

line, = plot(x,sin(x)) what does comma stand for?

like image 124
applicative_functor Avatar answered Sep 19 '22 03:09

applicative_functor


Use the "label" keyword, like so:

plt.plot(x, y, label='x vs. y') 

and then add the legend like so:

plt.legend() 

The legend will retain line properties like thickness, colours, etc.

enter image description here

like image 28
Ytsen de Boer Avatar answered Sep 21 '22 03:09

Ytsen de Boer