legends with fill_between

i can't make a legend for a fill_between and don't understand what i'm doing
wrong. the following code seems like the most obvious way to make a legend
for a fill_between, but does do what i expect. the code plots, but
complains that "No labeled objects found." any help would be much
appreciated.

from pylab import fill_between, legend, show
a = [0,1,2,4,5]
b = [1,1,1,1,1]
c = [0,1,2,4,5]
fill_between(a,b,c,label='hi')
legend()
show()

···

--
View this message in context: http://www.nabble.com/legends-with-fill_between-tp22816609p22816609.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

The matplotlib legend does not currently support fill_between. As a
matter of fact, fill_between command creates RegularPolyCollection
artist, and the mpl legend does not know how to handle this kind of
artist at this time. And I personally do not have good idea how to
draw a handle for this (a simple rectangle box like the handles for
the Patch?). Any suggestion is welcomed.

Meanwhile, you may draw an artist out of your axes area and use this
for the legend entry. For example, for simple a rectangle handle,

from pylab import fill_between, legend, show
a = [0,1,2,4,5]
b = [1,1,1,1,1]
c = [0,1,2,4,5]
fb = fill_between(a,b,c,label='hi')

from matplotlib.patches import Rectangle
r = Rectangle((0, 0), 1, 1) # creates rectangle patch for legend use.

legend([r], ["hi"])
show()

-JJ

···

On Tue, Mar 31, 2009 at 7:06 PM, empty83 <matt.terry@...287...> wrote:

i can't make a legend for a fill_between and don't understand what i'm doing
wrong. the following code seems like the most obvious way to make a legend
for a fill_between, but does do what i expect. the code plots, but
complains that "No labeled objects found." any help would be much
appreciated.

from pylab import fill_between, legend, show
a = [0,1,2,4,5]
b = [1,1,1,1,1]
c = [0,1,2,4,5]
fill_between(a,b,c,label='hi')
legend()
show()
--
View this message in context: http://www.nabble.com/legends-with-fill_between-tp22816609p22816609.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

I understand why things were not behaving as I expected and I don't really
have any good ideas on how to make a legend from a RegularPolyCollection in
general.

However, I do not grok why fill_between returns a RegularPolyCollection
rather than a Polygon (like fill does). Does fill_between(x,y0,y1) differ
substantially from fill(x+x[::-1], y0+y1[::-1])? To me this seems like the
most intuitive behavior.

-matt

Jae-Joon Lee wrote:

···

The matplotlib legend does not currently support fill_between. As a
matter of fact, fill_between command creates RegularPolyCollection
artist, and the mpl legend does not know how to handle this kind of
artist at this time. And I personally do not have good idea how to
draw a handle for this (a simple rectangle box like the handles for
the Patch?). Any suggestion is welcomed.

--
View this message in context: http://www.nabble.com/legends-with-fill_between-tp22816609p22834285.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Because fill_between handles masks, it uses a collection (a
PolyCollection, not a RegularPolyCollection) rather than a simple
Polygon. For example, see the use of the "where" argument in the
second plot at

  http://matplotlib.sourceforge.net/examples/pylab_examples/fill_between.html

The simplest way to handle this for the legend is probably to create a
proxy Rectangle with the same properties as the first element in the
poly collection. Since it is not designed to support multiple
properties for the different fill regions, we might be able to improve
this from a design and performance perspective by using a complex Path
instead of a PolyCollection.

In the interim, you can always manually create a legend using a proxy
Rectangle passed in as a handle to the legend, though admittedly this
is much more cumbersome.

JDH

···

On Wed, Apr 1, 2009 at 2:50 PM, empty83 <matt.terry@...287...> wrote:

I understand why things were not behaving as I expected and I don't really
have any good ideas on how to make a legend from a RegularPolyCollection in
general.

However, I do not grok why fill_between returns a RegularPolyCollection
rather than a Polygon (like fill does). Does fill_between(x,y0,y1) differ
substantially from fill(x+x[::-1], y0+y1[::-1])? To me this seems like the
most intuitive behavior.