我正在尝试在matplotlib中绘制一条线..我正在寻找正确的插值类型..我想要这样的东西
I am trying to plot a line in matplotlib.. I am searching for the right type of interpolation.. I want something like this
每行都经过平滑处理的位置.我尝试了scipy和matplotlib的几种组合,例如
where every line is smoothed. I tried several combination of scipy and matplotlib, such as
x_new = np.arange(x, x_length, 1) tck = interpolate.splrep(x, y, s=3) y_new = interpolate.splev(x_new, tck, der=0) ax.plot(x_new, y_new, color+lstyle)但是我得到的最好结果是
but the best result I get is
该行表示一个增加的变量 ..因此它是错误的表示形式.我可以搜索什么?
The line represents an increasing variable.. so it is a wrong representation. What can I search for?
谢谢
我正在考虑从我自己实现一个方法,但是我不知道它是否已经完成..伪代码如下
I am thinking about implementing a method from myself, but I don't know if it has been already done.. pseudo code is the following
take x and y calculate spline for each three points x[0], x[1], x[2] ... x[1], x[2], x[3] ... and so on for each y[n] sums every computation done for it and divide by number of computations (i.e. y[1] is computed for triplette x[0..2] and x[1..3] so the sum is divided by two (average for each point is taken as its value)推荐答案
对于该类型的图形,您需要单调插值. PchipInterpolator 类(您可以可以在scipy.interpolate中以其较短的别名pchip引用).
For that type of graph, you want monotonic interpolation. The PchipInterpolator class (which you can refer to by its shorter alias pchip) in scipy.interpolate can be used:
import numpy as np from scipy.interpolate import pchip import matplotlib.pyplot as plt # Data to be interpolated. x = np.arange(10.0) y = np.array([5.0, 10.0, 20.0, 15.0, 13.0, 22.0, 20.0, 15.0, 12.0, 16.0]) # Create the interpolator. interp = pchip(x, y) # Dense x for the smooth curve. xx = np.linspace(0, 9.0, 101) # Plot it all. plt.plot(xx, interp(xx)) plt.plot(x, y, 'bo') plt.ylim(0, 25) plt.grid(True) plt.show()结果:
更多推荐
如何使用numpy/scipy/matplotlib以最小的平滑度绘制线(多边形链)
发布评论