如何使用 numpy/scipy/matplotlib 以最小的平滑度绘制线(多边形链)

编程入门 行业动态 更新时间:2024-10-27 04:33:43
本文介绍了如何使用 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 以最小的平滑度绘制线(多边形链)

本文发布于:2023-08-02 00:07:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1272919.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多边形   平滑   如何使用   最小   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!