0

I'm trying to place a legend in a subfigure but I can't manage to do so. Here is an example of what I'm trying to do:

def test():
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    V = 10
    X = range (V)
    char = 'a'
    leg = []
    legp = []
    for i in range (0,5):
        Y = np.random.randn(V)
        ap = ax1.plot(X,Y)
        legp.append(ap)
        char = chr(ord(char)+1)
        leg.append(char)
    fig.legend(legp,leg)
    fig.show()

This yield with empty legend. I also get a bunch of warning messages:

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 [] 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),))

I guess this is something to do with this "proxy artist" but the link it points out to the link where learned this to begin with.

For those wondering, I want to include only part of the drawn plots in the legend.

How can I achieve this then?

Edit:
I'm using python 2.7.3 on Ubuntu 12.10 with gnome.

Yotam
  • 10,295
  • 30
  • 88
  • 128
  • Can you `print` (debug) `legp` and `leg` before calling `legend()`? Your warning tells you that one of these is an empty list (perhaps both), meaning that either your loop isn't properly run, or they `append` statements don't work as expected. (Also, double check that the code here is *exactly* the same as the code you.) –  Oct 24 '12 at 09:15
  • @Evert. I replaced ``ax1.plot(X,Y)`` with ``ax1.plot(X,Y)[0]`` and this work now. You can read gerogesl's and mine comment thread. Thank you. – Yotam Oct 24 '12 at 09:59

1 Answers1

3

Weird... It seems to work fine with me.

EDIT : The issue came from unpacking the plot return object : ap, = ax1.plot(X,Y)

For more info on the use of the comma :

Matplotlib Legends not working

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

Controling line properties

END OF EDIT

See :

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
V = 10
X = range (V)
char = 'a'
leg = []
legp = []
for i in range (0,5):
    Y = np.random.randn(V)
    ap = ax1.plot(X,Y)
    legp.append(ap)
    char = chr(ord(char)+1)
    leg.append(char)
fig.legend(legp,leg)
plt.show()

And the result : enter image description here

Community
  • 1
  • 1
lucasg
  • 10,734
  • 4
  • 35
  • 57
  • What python version are you using? What OS? – Yotam Oct 24 '12 at 08:39
  • I'm using python 2.7.3 with mathplotlib v1.1.0 on Windows. – lucasg Oct 24 '12 at 08:53
  • Can you think of an alternative method to achieve this? – Yotam Oct 24 '12 at 09:17
  • not without more info on your error ( what kind of subplot, ... ). But here is a wild guess : [Matplotlib Legends not working](http://stackoverflow.com/questions/11983024/matplotlib-legends-not-working) – lucasg Oct 24 '12 at 09:26
  • Yes, the comma did the trick. What is the meaning of the comma? – Yotam Oct 24 '12 at 09:32
  • `matplotlib.pyplot.plot` return a list of line, and in your case it is a single-element list ( like `tuple([1]) = (1,)` ). the comma is here to unpack it and return only the first element. – lucasg Oct 24 '12 at 09:41
  • OK. I replaced the comma with ``ap = ax1.plot(X,Y)[0]``. This seems more logical to me. – Yotam Oct 24 '12 at 09:54