From: John Hunter <jdhunter@ac...> - 2004-07-09 12:02:27
becker> however the legend is incorrect/incomplete if i change the order the
becker> 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,
From: Andrew Becker <becker@da...> - 2004-07-09 17:36:14
Hi - By passing the list of lines, the problem is solved, thanks! It
is the case though that the intelligent guess does fail when you do an
'errorbar plot' sequence but not when you do a 'plot errorbar'
sequence. In this first case, the legend associated with the 'plot'
is always a straight black line no matter what the requested format.
Thanks for the help,
ps. am using matplotlib-0.60.1
From: Peter Groszkowski <pgroszko@ge...> - 2004-07-09 18:20:59
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