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?
-- Russell