Legend for stacked histogram?

In article <rowen-3539BF.13542730062011@...1803...>,

I am trying to make a legend for a stacked histogram using matplotlib
1.0.1 and it's not working.

Here's what I've tried so far:

    count, bins, ignored = pyplot.hist(
        (matchedStarPsfMags, unmatchedRefStarPsfMags,
          unmatchedSourcePsfMags),
        bins=30, histtype='barstacked', normed=True)
    pyplot.legend(("matched stars", "unmatched stars", \
      "false detections"), loc='upper left')

This produces a nice stacked histogram with red, green and blue.
Unfortunately the legend is blue for all three entries, so the legend is
useless!

I figured I could label the data instead. The documentation for hist
says:
label:
String, or sequence of strings to match multiple datasets. Bar charts
yield multiple patches per dataset, but only the first gets the label,
so that the legend command will work as expected:

That last sentence sounded really ominous in this context, but I figured
I would try it anyway. Unfortunately this code fails:

    count, bins, ignored = pyplot.hist(
        (matchedStarPsfMags, unmatchedRefStarPsfMags,
           unmatchedSourcePsfMags),
        label = ("matched stars", "unmatched stars",
            "false detections"),
        bins=30, histtype='barstacked', normed=True)
    pyplot.legend(loc='upper left')

with this error:

Traceback (most recent call last):
  File "bin/measDepth.py", line 291, in <module>
    pyplot.legend(loc='upper left')
  File
"/lsst/DC3/stacks/gcc443/15oct2010/Linux64/external/matplotlib/0.98.5.2+1
/lib/python/matplotlib/pyplot.py", line 2441, in legend
    ret = gca().legend(*args, **kwargs)
  File
"/lsst/DC3/stacks/gcc443/15oct2010/Linux64/external/matplotlib/0.98.5.2+1
/lib/python/matplotlib/axes.py", line 3777, in legend
    label != '' and not label.startswith('_')):
AttributeError: 'tuple' object has no attribute 'startswith'

In other words the documentation appears to be incorrect that a sequence
of strings is acceptable.

Any suggestions?

Oops. I was able to answer my own question.

It turns out I was using an ancient version of matplotlib (0.98.5.2) (I
was using a remote server and forgot to check).

The second version does work with matplotlib 1.0.1 and produces a nice
legend with the correct color for each entry. Yaay!

The first version produces a useless legend with all colors the same on
both modern matplotlib and the ancient matplotlib. So use the second
method of specifying label=(...) in the hist command.

-- Russell

···

"Russell E. Owen" <rowen@...2756...> wrote: