ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 24 Nov 2011 03:22:09 -0600interpolated 2D plothttps://ask.sagemath.org/question/8471/interpolated-2d-plot/ Hello!
Is there a way to create a plot from a list of data using some kind of non-linear interpolation (e.g. splines) between the data points?
Thanks.Mon, 14 Nov 2011 10:10:36 -0600https://ask.sagemath.org/question/8471/interpolated-2d-plot/Answer by DSM for <p>Hello!
Is there a way to create a plot from a list of data using some kind of non-linear interpolation (e.g. splines) between the data points?</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/8471/interpolated-2d-plot/?answer=12900#post-id-12900Sure, there are a couple of ways. Here are two:
import numpy
from scipy import interpolate
data = [(x, sin(x)**2+(x % 2.7)**3) for x in numpy.linspace(0, 5, 20)]
# separate data into x and y components
xx,yy = zip(*data)
# build spline in two ways..
scipy_spline = interpolate.InterpolatedUnivariateSpline(xx, yy)
gsl_spline = spline(data)
p = list_plot(data, color="green")
p += plot(scipy_spline, (0, 5), color="red")
p += plot(gsl_spline, (0, 5), linestyle='--', color="blue")
show(p)
Note that if you build the spaces yourself and use the built-in spline object you wouldn't need to import anything.
![image description](/upfiles/13213094628909337.png)
Mon, 14 Nov 2011 10:24:59 -0600https://ask.sagemath.org/question/8471/interpolated-2d-plot/?answer=12900#post-id-12900Comment by v_2e for <p>Sure, there are a couple of ways. Here are two:</p>
<pre><code>import numpy
from scipy import interpolate
data = [(x, sin(x)**2+(x % 2.7)**3) for x in numpy.linspace(0, 5, 20)]
# separate data into x and y components
xx,yy = zip(*data)
# build spline in two ways..
scipy_spline = interpolate.InterpolatedUnivariateSpline(xx, yy)
gsl_spline = spline(data)
p = list_plot(data, color="green")
p += plot(scipy_spline, (0, 5), color="red")
p += plot(gsl_spline, (0, 5), linestyle='--', color="blue")
show(p)
</code></pre>
<p>Note that if you build the spaces yourself and use the built-in spline object you wouldn't need to import anything.</p>
<p><img alt="image description" src="/upfiles/13213094628909337.png"/></p>
https://ask.sagemath.org/question/8471/interpolated-2d-plot/?comment=20834#post-id-20834Thanks a lot!
I guess, "gsl_spline = spline(data)" is exactly what I need. :)Thu, 24 Nov 2011 03:22:09 -0600https://ask.sagemath.org/question/8471/interpolated-2d-plot/?comment=20834#post-id-20834