legend bug

however the legend is incorrect/incomplete if i change the order the

   > plots are done

   sp = matlib.subplot(111)
   ctio = sp.errorbar(jd, f, yerr=df, fmt='bo', ecolor='k', capsize=1)
   mlfit = sp.plot(mljd, mlflux, 'r-')
   snfit = sp.plot(mljd, mlflux, 'g-')
   sp.legend( ('ML fit', 'SN fit', 'CTIO'), loc='upper right')

I hesitate to call this a bug. I have been hard at work on the
matplotlib.mind_reading module but it is incomplete. That is,
matplotlib does not know what you want the legend to look like, you
have to tell it.

It does try to make an intelligent guess if you don't tell it, and it
guesses by assuming the order the labels you give is the same as the
order of the plot commands. If you want otherwise, you need to pass
it an explicit list of line or patch handles. Something like

    # errorbar returns two values, the plot line and the list of
    # errorbar lines
    ctio, errlines = sp.errorbar(jd, f, yerr=df, fmt='bo', ecolor='k', capsize=1)

    # note the comma in the return value. plot returns *a list of
    # lines*. Since you have a length one list, I'm extracting the
    # first element of the list using tuple unpacking
    mlfit, = sp.plot(mljd, mlflux, 'r-')
    snfit, = sp.plot(mljd, mlflux, 'g-')

    # by passing a list of lines, you can control the order
    sp.legend( (mlfit, snfit, ctio), ('ML fit', 'SN fit', 'CTIO'), loc='upper right')

See http://matplotlib.sf.net/examples/legend_demo2.py for an example;
all the examples can also be found in the examples subdirectory of the
matplotlib src distribution, *.tar.gz or *.zip.

Hope this helps,
JDH

John Hunter wrote:

I hesitate to call this a bug. I have been hard at work on the
matplotlib.mind_reading module but it is incomplete.

c'mon John... it's about time you this thing done! );

···

--
Peter Groszkowski Gemini Observatory
Tel: +1 808 974-2509 670 N. A'ohoku Place
Fax: +1 808 935-9235 Hilo, Hawai'i 96720, USA