ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 04 Jan 2012 08:57:54 -0600Fill area under list_plothttp://ask.sagemath.org/question/8593/fill-area-under-list_plot/ Hello!
Is there a way to fill the area between the two curves plotted using the list_plot() function?
My particular application is this: I have a list of points of the format *(x, y, y_max, y_min)*, where *y_max* and *y_min* denote the maximum and minimum values of *y* for the given *x* value because of the measurements uncertainty. I need to plot this curve and fill the uncertainty corridor with some other color.
I use my own function that retrieves the data from this list and use *plot()* to obtain an abovementioned result at the moment. But in this case my function is evaluated in some kind of [random points](http://groups.google.com/group/sage-support/browse_thread/thread/45803c04598182ee) which do not correspond the exact experimental *x* values from the initial data list, that is why this solution is not very good too.
Thank you.Sat, 24 Dec 2011 01:32:33 -0600http://ask.sagemath.org/question/8593/fill-area-under-list_plot/Comment by niles for <p>Hello!</p>
<p>Is there a way to fill the area between the two curves plotted using the list_plot() function?</p>
<p>My particular application is this: I have a list of points of the format <em>(x, y, y_max, y_min)</em>, where <em>y_max</em> and <em>y_min</em> denote the maximum and minimum values of <em>y</em> for the given <em>x</em> value because of the measurements uncertainty. I need to plot this curve and fill the uncertainty corridor with some other color.</p>
<p>I use my own function that retrieves the data from this list and use <em>plot()</em> to obtain an abovementioned result at the moment. But in this case my function is evaluated in some kind of <a href="http://groups.google.com/group/sage-support/browse_thread/thread/45803c04598182ee">random points</a> which do not correspond the exact experimental <em>x</em> values from the initial data list, that is why this solution is not very good too.</p>
<p>Thank you.</p>
http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?comment=20631#post-id-20631what happened to @DSM's answer?Mon, 26 Dec 2011 02:41:01 -0600http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?comment=20631#post-id-20631Answer by DSM for <p>Hello!</p>
<p>Is there a way to fill the area between the two curves plotted using the list_plot() function?</p>
<p>My particular application is this: I have a list of points of the format <em>(x, y, y_max, y_min)</em>, where <em>y_max</em> and <em>y_min</em> denote the maximum and minimum values of <em>y</em> for the given <em>x</em> value because of the measurements uncertainty. I need to plot this curve and fill the uncertainty corridor with some other color.</p>
<p>I use my own function that retrieves the data from this list and use <em>plot()</em> to obtain an abovementioned result at the moment. But in this case my function is evaluated in some kind of <a href="http://groups.google.com/group/sage-support/browse_thread/thread/45803c04598182ee">random points</a> which do not correspond the exact experimental <em>x</em> values from the initial data list, that is why this solution is not very good too.</p>
<p>Thank you.</p>
http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?answer=13065#post-id-13065I'm not sure I follow, but were you thinking of something like this?
# make data
set_random_seed(3)
yf(x) = 5*x+2
yrand = 0.5
yunc_abs = 1.0
xx = [0.05*i for i in range(50)]
yy = [yf(x) + gauss(0, yrand) for x in xx]
ymins = [y-yunc_abs for y in yy]
ymaxs = [y+yunc_abs for y in yy]
# make spline approximation to boundaries
low_fn = spline(zip(xx,ymins))
upp_fn = spline(zip(xx,ymaxs))
# add points
p = list_plot(zip(xx,yy))
# add band
p += plot(low_fn, fill=upp_fn, xmin=min(xx), xmax=max(xx), alpha=0.0)
show(p)
![unc band](/upfiles/13247494472996722.png)Sat, 24 Dec 2011 05:57:48 -0600http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?answer=13065#post-id-13065Comment by v_2e for <p>I'm not sure I follow, but were you thinking of something like this?</p>
<pre><code># make data
set_random_seed(3)
yf(x) = 5*x+2
yrand = 0.5
yunc_abs = 1.0
xx = [0.05*i for i in range(50)]
yy = [yf(x) + gauss(0, yrand) for x in xx]
ymins = [y-yunc_abs for y in yy]
ymaxs = [y+yunc_abs for y in yy]
# make spline approximation to boundaries
low_fn = spline(zip(xx,ymins))
upp_fn = spline(zip(xx,ymaxs))
# add points
p = list_plot(zip(xx,yy))
# add band
p += plot(low_fn, fill=upp_fn, xmin=min(xx), xmax=max(xx), alpha=0.0)
show(p)
</code></pre>
<p><img alt="unc band" src="/upfiles/13247494472996722.png"/></p>
http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?comment=20634#post-id-20634Yes, it looks very close to what I need, except that I do not need the splines, because in my case the experimental data and the smoothed data are different things, and I want to show this difference. Is there a way to generate some kind of function from the lists of points and to evaluate this function exactly at the points corresponding to the X-points of the initial list or at least at the same X-points every time. The problem in my case is that 'list_plot' and 'plot' do not place the points at the same X-values.Sun, 25 Dec 2011 02:09:32 -0600http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?comment=20634#post-id-20634Answer by achrzesz for <p>Hello!</p>
<p>Is there a way to fill the area between the two curves plotted using the list_plot() function?</p>
<p>My particular application is this: I have a list of points of the format <em>(x, y, y_max, y_min)</em>, where <em>y_max</em> and <em>y_min</em> denote the maximum and minimum values of <em>y</em> for the given <em>x</em> value because of the measurements uncertainty. I need to plot this curve and fill the uncertainty corridor with some other color.</p>
<p>I use my own function that retrieves the data from this list and use <em>plot()</em> to obtain an abovementioned result at the moment. But in this case my function is evaluated in some kind of <a href="http://groups.google.com/group/sage-support/browse_thread/thread/45803c04598182ee">random points</a> which do not correspond the exact experimental <em>x</em> values from the initial data list, that is why this solution is not very good too.</p>
<p>Thank you.</p>
http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?answer=13052#post-id-13052Maybe you should try without list_plot?
(no random points)
Replace li by your list of [x,y(x),ymax(x),ymin(x)]
li=[[x,sin(x),sin(x)+0.2,sin(x)-0.2] for x in srange(0,2*pi,0.3)]
a=[[x[0],x[2]] for x in li]
b=[[x[0],x[3]] for x in li]
c=[[x[0],x[1]] for x in li]
b.reverse()
p1=polygon2d(a+b,color='lightgrey',alpha=0.7)
p2=line(c,thickness=3)
p3=line(a,color='black')
p4=line(b,color='black')
(p1+p2+p3+p4).show()
Sun, 25 Dec 2011 04:48:43 -0600http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?answer=13052#post-id-13052Comment by v_2e for <p>Maybe you should try without list_plot?</p>
<p>(no random points)</p>
<p>Replace li by your list of [x,y(x),ymax(x),ymin(x)]</p>
<p>li=[[x,sin(x),sin(x)+0.2,sin(x)-0.2] for x in srange(0,2*pi,0.3)]</p>
<p>a=[[x[0],x[2]] for x in li]</p>
<p>b=[[x[0],x[3]] for x in li]</p>
<p>c=[[x[0],x[1]] for x in li]</p>
<p>b.reverse()</p>
<p>p1=polygon2d(a+b,color='lightgrey',alpha=0.7)</p>
<p>p2=line(c,thickness=3)</p>
<p>p3=line(a,color='black')</p>
<p>p4=line(b,color='black')</p>
<p>(p1+p2+p3+p4).show()</p>
http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?comment=20590#post-id-20590Thanks! Not bad workaround.Wed, 04 Jan 2012 08:57:54 -0600http://ask.sagemath.org/question/8593/fill-area-under-list_plot/?comment=20590#post-id-20590