How might create a legend for an area chart made with PolyCollection objects
via the fill_between method?  For example, on the following chart,

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ind = np.arange(3)
y1 = np.array([1,2,3])
y2 = y1 * 2
a = ax.fill_between(ind, y1, np.zeros(len(ind)), facecolor='r', label='a')
b = ax.fill_between(ind, y2, y1, facecolor='b', label='b')
plt.show()

I would like to create a legend that displays the label for a and b and the
corresponding fill color.  Unlike with non-collections objects, when I call
ax.legend() I get an AttributeError and the following output,

AttributeError                            Traceback (most recent call last)

/home/chuck/<ipython console> in <module>()

/home/chuck/matplotlib/trunk/matplotlib/lib/matplotlib/axes.pyc in
legend(self, *args, **kwargs)
   4004
   4005         handles = cbook.flatten(handles)
-> 4006         self.legend_ = mlegend.Legend(self, handles, labels,
**kwargs)
   4007         return self.legend_
   4008

/home/chuck/matplotlib/trunk/matplotlib/lib/matplotlib/legend.pyc in
__init__(self, parent, handles, labels, loc, numpoints, markerscale,
scatterpoints, scatteryoffsets, prop, pad, labelsep, handlelen,
handletextsep, axespad, borderpad, labelspacing, handlelength,
handletextpad, borderaxespad, columnspacing, ncol, mode, fancybox, shadow,
title, bbox_to_anchor, bbox_transform)
    307
    308         # init with null renderer
--> 309         self._init_legend_box(handles, labels)
    310
    311         self.set_title(title)

/home/chuck/matplotlib/trunk/matplotlib/lib/matplotlib/legend.pyc in
_init_legend_box(self, handles, labels)
    584
    585             handle = handle_list[-1]
--> 586             handlebox.add_artist(handle)
    587             if hasattr(handle, "_legmarker"):
    588                 handlebox.add_artist(handle._legmarker)

/home/chuck/matplotlib/trunk/matplotlib/lib/matplotlib/offsetbox.pyc in
add_artist(self, a)
    489         'Add any :class:`~matplotlib.artist.Artist` to the container
box'
    490         self._children.append(a)
--> 491         a.set_transform(self.get_transform())
    492
    493

AttributeError: 'NoneType' object has no attribute 'set_transform'
------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to