## Re: [Matplotlib-users] fill area between two curves? fill_between?

 Re: [Matplotlib-users] fill area between two curves? fill_between? From: John Hunter - 2004-09-28 13:04:37 ```>>>>> "Dirk" == 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 ```

 [Matplotlib-users] fill area between two curves? fill_between? From: - 2004-09-28 11:44:09 ```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()? -Dirk ________________________________________________________________ Verschicken Sie romantische, coole und witzige Bilder per SMS! Jetzt neu bei WEB.DE FreeMail: http://freemail.web.de/?mc=021193 ```
 Re: [Matplotlib-users] fill area between two curves? fill_between? From: John Hunter - 2004-09-28 13:04:37 ```>>>>> "Dirk" == 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 ```
 Re: [Matplotlib-users] fill area between two curves? fill_between? From: Andrew Straw - 2004-09-28 16:15:27 ```On Sep 28, 2004, at 5:15 AM, John Hunter wrote: >>>>>> "Dirk" == 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 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() ```
 Re: [Matplotlib-users] fill area between two curves? fill_between? From: John Hunter - 2004-09-28 16:38:38 ```>>>>> "Andrew" == Andrew Straw writes: Andrew> The fill() command itself does work if you give it Andrew> coordinates for a closed polygon. This draws a filled Andrew> spiral: Of course (slaps self on forehead). So my example rewritten would be rewritten using the cleaner from matplotlib.matlab import * x1 = arange(0, 2, 0.01) y1 = sin(2*pi*x1) y2 = sin(4*pi*x1) + 2 # reverse x and y2 so the polygon fills in order x = concatenate( (x1,x1[::-1]) ) y = concatenate( (y1,y2[::-1]) ) p = fill(x, y, facecolor='g') set(p, alpha=0.5) show() I took the liberty of adding your example to CVS in the examples subdir as examples/fill_spiral.py. Thanks! JDH ```
 Re: [Matplotlib-users] fill area between two curves? fill_between? From: - 2004-09-28 16:09:58 ```John Hunter: Normally I wouldn't waste bandwith in a mailing-list by sending neither a question nor an answer, but in this case I feel the urge to thank you for your support. Your function does exactly what I need and you even gave a hint on how to implement such functional extensions. On top of that, the answer came an hour after I sent the question :-) > 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. [...] Do I have to dive into the class description at http://matplotlib.sourceforge.net/classdocs.html to get this informations or is there a more user-friendly description of the matplotlib internals? Thanks Dirk ----- Original Message ----- From: "John Hunter" To: Cc: Sent: Tuesday, September 28, 2004 2:15 PM Subject: Re: [Matplotlib-users] fill area between two curves? fill_between? > >>>>> "Dirk" == 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 > ```
 Re: [Matplotlib-users] fill area between two curves? fill_between? From: John Hunter - 2004-09-28 17:37:39 ```>>>>> "Dirk" == writes: Dirk> John Hunter: Normally I wouldn't waste bandwith in a Dirk> mailing-list by sending neither a question nor an answer, Dirk> but in this case I feel the urge to thank you for your Dirk> support. Your function does exactly what I need and you even Dirk> gave a hint on how to implement such functional Dirk> extensions. On top of that, the answer came an hour after I Dirk> sent the question :-) Your welcome... Dirk> Do I have to dive into the class description at Dirk> http://matplotlib.sourceforge.net/classdocs.html to get this Dirk> informations or is there a more user-friendly description of Dirk> the matplotlib internals? I think the best way, after studying the examples in the examples directory of the src distribution, is to read through the code in axes.py. Almost all of the plotting functions in matplotlib.matlab are wrappers for axes plotting functions. Eg scatter wraps matplotlib.Axes.scatter, plot wraps matplotlib.Axes.plot and so on. Most of the axes plotting functions in turn create the primitive objects that actually make up the plot. Axes.plot is one of the more complicated functions, since it does a lot of variable length argument processing, so it is probably not the best place to start. If you have a link to the classdocs open, as you read through the axes code, you can read the docs for the functions that are being called. JDH ```
 Re: [Matplotlib-users] fill area between two curves? fill_between? From: - 2004-09-29 15:51:53 ```On Tuesday 28 September 2004 16:48, John Hunter wrote: > >>>>> "Dirk" == writes: > > Dirk> John Hunter: Normally I wouldn't waste bandwith in a > Dirk> mailing-list by sending neither a question nor an answer, > Dirk> but in this case I feel the urge to thank you for your > Dirk> support. Your function does exactly what I need and you even > Dirk> gave a hint on how to implement such functional > Dirk> extensions. On top of that, the answer came an hour after I > Dirk> sent the question :-) > > Your welcome... Hi John, I agree with Dirk on the precision and promptness of your responses to all our questions. I am always learning new things about matplolib (MPL) through theses Q&A exchanges. But since topic of bandwidth usage came up in this thread, for the sake of saving not only Sourceforge's but also JDH's bandwidth, I believe a documentation project for MPL should be started. I understand that you might not want to commit effort to document MPL now, since it's still under pretty intense development. However, starting a Wiki documentation project might be a good idea, since the user community could help by adding short tutorials and recipes derived from their own experience with MPL. Moreover, the wiki could slowly evolve into a full blown documentation project. What do you think? cheers, Flavio --- ```