Python/Matplotlib光滑曲线画图---Scipy库函数使用
来源:互联网 发布:spss modeler mac下载 编辑:程序博客网 时间:2024/05/23 17:29
转载地址:https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html
Interpolation (scipy.interpolate
)
Contents
- Interpolation (
scipy.interpolate
)- 1-D interpolation (
interp1d
) - Multivariate data interpolation (
griddata
) - Spline interpolation
- Spline interpolation in 1-d: Procedural (interpolate.splXXX)
- Spline interpolation in 1-d: Object-oriented (
UnivariateSpline
) - Two-dimensional spline representation: Procedural (
bisplrep
) - Two-dimensional spline representation: Object-oriented (
BivariateSpline
)
- Using radial basis functions for smoothing/interpolation
- 1-d Example
- 2-d Example
- 1-D interpolation (
There are several general interpolation facilities available in SciPy, for data in 1, 2, and higher dimensions:
- A class representing an interpolant (
interp1d
) in 1-D, offering several interpolation methods. - Convenience function
griddata
offering a simple interface to interpolation in N dimensions (N = 1, 2, 3, 4, ...). Object-oriented interface for the underlying routines is also available. - Functions for 1- and 2-dimensional (smoothed) cubic-spline interpolation, based on the FORTRAN library FITPACK. There are both procedural and object-oriented interfaces for the FITPACK library.
- Interpolation using Radial Basis Functions.
1-D interpolation (interp1d
)
The interp1d
class in scipy.interpolate
is a convenient method to create a function based on fixed data points which can be evaluated anywhere within the domain defined by the given data using linear interpolation. An instance of this class is created by passing the 1-d vectors comprising the data. The instance of this class defines a __call__ method and can therefore by treated like a function which interpolates between known data values to obtain unknown values (it also has a docstring for help). Behavior at the boundary can be specified at instantiation time. The following example demonstrates its use, for linear and cubic spline interpolation:
(Source code)
Multivariate data interpolation (griddata
)
Suppose you have multidimensional data, for instance for an underlying function f(x, y) you only know the values at points (x[i], y[i]) that do not form a regular grid.
Suppose we want to interpolate the 2-D function
on a grid in [0, 1]x[0, 1]
but we only know its values at 1000 data points:
This can be done with griddata
– below we try out all of the interpolation methods:
One can see that the exact result is reproduced by all of the methods to some degree, but for this smooth function the piecewise cubic interpolant gives the best results:
(Source code)
Spline interpolation
Spline interpolation in 1-d: Procedural (interpolate.splXXX)
Spline interpolation requires two essential steps: (1) a spline representation of the curve is computed, and (2) the spline is evaluated at the desired points. In order to find the spline representation, there are two different ways to represent a curve and obtain (smoothing) spline coefficients: directly and parametrically. The direct method finds the spline representation of a curve in a two- dimensional plane using the function splrep
. The first two arguments are the only ones required, and these provide the
For curves in splprep
allows defining the curve parametrically. For this function only 1 input argument is required. This input is a list of
The keyword argument, s , is used to specify the amount of smoothing to perform during the spline fit. The default value of
Once the spline representation of the data has been determined, functions are available for evaluating the spline (splev
) and its derivatives (splev
, spalde
) at any point and the integral of the spline between any two points ( splint
). In addition, for cubic splines ( sproot
). These functions are demonstrated in the example that follows.
Cubic-spline
(Source code)
Derivative of spline
Integral of spline
Roots of spline
Notice that sproot
failed to find an obvious solution at the edge of the approximation interval,
Parametric spline
Spline interpolation in 1-d: Object-oriented (UnivariateSpline
)
The spline-fitting capabilities described above are also available via an objected-oriented interface. The one dimensional splines are objects of the UnivariateSpline
class, and are created with the __call__
, allowing the object to be called with the x-axis values at which the spline should be evaluated, returning the interpolated y-values. This is shown in the example below for the subclass InterpolatedUnivariateSpline
. The integral
, derivatives
, androots
methods are also available on UnivariateSpline
objects, allowing definite integrals, derivatives, and roots to be computed for the spline.
The UnivariateSpline class can also be used to smooth data by providing a non-zero value of the smoothing parameter s
, with the same meaning as the s
keyword of the splrep
function described above. This results in a spline that has fewer knots than the number of data points, and hence is no longer strictly an interpolating spline, but rather a smoothing spline. If this is not desired, the InterpolatedUnivariateSpline
class is available. It is a subclass of UnivariateSpline
that always passes through all points (equivalent to forcing the smoothing parameter to 0). This class is demonstrated in the example below.
The LSQUnivariateSpline
class is the other subclass of UnivariateSpline
. It allows the user to specify the number and location of internal knots explicitly with the parameter t. This allows creation of customized splines with non-linear spacing, to interpolate in some domains and smooth in others, or change the character of the spline.
InterpolatedUnivariateSpline
(Source code)
LSQUnivarateSpline with non-uniform knots
- Python/Matplotlib光滑曲线画图---Scipy库函数使用
- 使用python matplotlib画图
- python使用matplotlib画图
- Python使用matplotlib,numpy,scipy进行散点的平滑曲线化方法
- Python使用matplotlib,numpy,scipy进行散点的平滑曲线化方法
- python使用pip安装SciPy、SymPy、matplotlib
- python matplotlib画图安装和使用
- Python画图库 matplotlib, 数值计算库 numpy, 科学计算库 scipy 的安装
- Python画图库 matplotlib, 数值计算库 numpy, 科学计算库 scipy 的安装
- python使用安装numpy、scipy、matplotlib等工具包
- Python matplotlib 画图相关
- python matplotlib 画图简介
- Python Matplotlib画图示例
- python用matplotlib画图
- python画图:matplotlib(1)
- 【python】matplotlib画图
- python matplotlib 画图
- Python matplotlib画图表
- Android Studio发布项目到jcenter,一行代码引入Module
- js获取网页高度
- cocos2d-x 3.0的lua-binding接口导出
- 简谈高通Trustzone的实现
- python爬虫学习01
- Python/Matplotlib光滑曲线画图---Scipy库函数使用
- Lua的upvalue和闭包
- mybatis批量增加返回id值
- Appium基础篇4-如何解决每次运行脚本都安装apk的问题
- windows中使用Python进行AES加密解密-文本文件加密工具
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 C: Coconut
- 出现 org.apache.ibatis.binding.BindingException 异常的原因及解决方法
- CodeForces
- 京东2018校招研发笔试题记录