 # fill area between two curves? fill_between?

I want to fill the area between two curves with a color

> (incl. alpha channel, I use the agg backend). fill() seems
> to fill only the area between the x-axis and one curve - at
> least I found no way for filling only the area between two
> graphs. Is there a way for doing so at all? Or do I have to
> use another graphic library for this kind of fill_between()?

All of the matplotlib plot commands create primitive objects
(lines.Line2D, patches.Rectangle, patches.Polygon, text.Text). If a
plot command doesn't suit your needs, you can roll your own by
creating the right kind of primitive object. Here is a fill_between
implementation

from matplotlib.matlab import *
from matplotlib.patches import Polygon

def fill_between(ax, x, y1, y2, **kwargs):
# add x,y2 in reverse order for proper polygon filling
verts = zip(x,y1) + [(x[i], y2[i]) for i in range(len(x)-1,-1,-1)]
poly = Polygon(verts, **kwargs)
ax.autoscale_view()
return poly

x = arange(0, 2, 0.01)
y1 = sin(2*pi*x)
y2 = sin(4*pi*x) + 2
ax = gca()

p = fill_between(ax, x, y1, y2, facecolor='g')
p.set_alpha(0.5)
show()

Hope this helps,
JDH

The fill() command itself does work if you give it coordinates for a closed polygon. This draws a filled spiral:

from matplotlib.matlab import *
from numarray import *

theta = arange(0,8*pi,0.1)
a=1
b=.2

for dt in arange(0,2*pi,pi/2.0):

x = a*cos( theta+dt )*exp(b*theta)
y = a*sin( theta+dt )*exp(b*theta)

dt = dt+pi/4.0

x2 = a*cos( theta+dt )*exp(b*theta)
y2 = a*sin( theta+dt )*exp(b*theta)

xf = concatenate( (x,x2[::-1]) )
yf = concatenate( (y,y2[::-1]) )

p1=fill(xf,yf)

show()

···

On Sep 28, 2004, at 5:15 AM, John Hunter wrote:

> I want to fill the area between two curves with a color
> (incl. alpha channel, I use the agg backend). fill() seems
> to fill only the area between the x-axis and one curve - at
> least I found no way for filling only the area between two
> graphs. Is there a way for doing so at all? Or do I have to
> use another graphic library for this kind of fill_between()?

All of the matplotlib plot commands create primitive objects
(lines.Line2D, patches.Rectangle, patches.Polygon, text.Text). If a
plot command doesn't suit your needs, you can roll your own by
creating the right kind of primitive object. Here is a fill_between
implementation