如何使用 numpy/scipy/matplotlib 以最小的平滑度绘制线(多边形链) [英] How to plot line (polygonal chain) with numpy/scipy/matplotlib with minimal smoothing

查看:29
本文介绍了如何使用 numpy/scipy/matplotlib 以最小的平滑度绘制线(多边形链)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 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 以最小的平滑度绘制线(多边形链)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆