legend label for fill_between (or similar)

Hi All,

Sometimes, instead of using data points with error bars, I instead use fill_between to create a little bar, with a band which I use alpha=.3 or so.

I have tried unsuccessfully to find an easy way to create a legend label for this band - I am trying to have a similar band appear in my legend.

I am not attached to fill_between if there is a similar way to create such a "little bar" to represent my data point.

Thanks,

Andre

Hi Andre,

Unfortunately, I think the preferred method is to create a proxy artist:

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

Basically, you draw a fake patch with the same parameters as your fill (see example below).

Hope that helps,

-Tony

example from http://comments.gmane.org/gmane.comp.python.matplotlib.general/29476

def fill_between(x, y1, y2=0, ax=None, **kwargs):

“”"Plot filled region between y1 and y2.

This function works exactly the same as matplotlib’s fill_between, except

that it also plots a proxy artist (specifically, a rectangle of 0 size)

so that it can be added it appears on a legend.

“”"

ax = ax if ax is not None else plt.gca()

ax.fill_between(x, y1, y2, **kwargs)

p = plt.Rectangle((0, 0), 0, 0, **kwargs)

ax.add_patch(p)

return p

···

On Thu, Jun 21, 2012 at 4:09 PM, Andre’ Walker-Loud <walksloud@…287…> wrote:

Hi All,

Sometimes, instead of using data points with error bars, I instead use fill_between to create a little bar, with a band which I use alpha=.3 or so.

I have tried unsuccessfully to find an easy way to create a legend label for this band - I am trying to have a similar band appear in my legend.

I am not attached to fill_between if there is a similar way to create such a “little bar” to represent my data point.

Thanks,

Andre


Hi Tony,

Unfortunately, I think the preferred method is to create a proxy artist:

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

Basically, you draw a fake patch with the same parameters as your fill (see example below).

Hope that helps,

Yes, that helps. I also found another simple way using matplotlib.pyplot.bar()

···

===
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1,2])
data = np.array([10,8])
err = np.array([2,1])

b1 = plt.bar(x-.2,2*err,0.4,color='b',bottom=data - err,alpha=0.3)
plt.legend([b1[0]], ['nice legend graphic'],shadow=True,fancybox=True,numpoints=1)
plt.axis([0,3,0,15])

plt.show()

Thanks,

Andre