|
From: John H. <jdh...@ac...> - 2004-09-28 13:04:37
|
>>>>> "Dirk" == <rep...@we...> writes:
Dirk> I want to fill the area between two curves with a color
Dirk> (incl. alpha channel, I use the agg backend). fill() seems
Dirk> to fill only the area between the x-axis and one curve - at
Dirk> least I found no way for filling only the area between two
Dirk> graphs. Is there a way for doing so at all? Or do I have to
Dirk> 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.add_patch(poly)
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
|