我正在尝试在 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)但我得到的最好结果是
这条线代表一个递增的变量...所以这是一个错误的表示.我可以搜索什么?
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 以最小的平滑度绘制线(多边形链)
发布评论