In article <rowen-3539bf.13542730062...@news.gmane.org>,
 "Russell E. Owen" <ro...@uw.edu> wrote:

> 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


------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security 
threats, fraudulent activity, and more. Splunk takes this data and makes 
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to