在三个值之间插入曲线 [英] interpolate curve between three values

查看:44
本文介绍了在三个值之间插入曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本可以绘制图形:

x = np.array([0,1,2])y = np.array([5,4.31,4.01])plt.plot(x, y)plt.show()

问题是,直线从一点到另一点都是笔直的,但是我想平滑点之间的直线.

如果我使用 scipy.interpolate.spline 来平滑我的数据,我会得到以下结果:

 顺序= np.array([0,1,2])y = np.array([5, 4.31, 4.01])xnew = np.linspace(order.min(),order.max(),300)平滑 = 样条(顺序,y,xnew)plt.plot(xnew,平滑)plt.show()

但我想得到与给定的结果相同的结果

I have the following script that plots a graph:

x = np.array([0,1,2])
y = np.array([5, 4.31, 4.01])
plt.plot(x, y)
plt.show()

The problem is, that the line goes straight from point to point, but I want to smooth the line between the points.

If I use scipy.interpolate.spline to smooth my data I got following result:

 order = np.array([0,1,2])
 y = np.array([5, 4.31, 4.01])
 xnew = np.linspace(order.min(), order.max(), 300)
 smooth = spline(order, y, xnew)
 plt.plot(xnew, smooth)
 plt.show()

But I want to have the same result like in that given example

解决方案

If you use more points than 3 you will get the same result as in the linked question. There are many ways a spline of order 3 can go through 3 points.

But you may of course reduce the order to 2.

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import spline

x = np.array([0,1,2])
y = np.array([5, 4.31, 4.01])
plt.plot(x, y)

xnew = np.linspace(x.min(), x.max(), 300)
smooth = spline(x, y, xnew, order=2)
plt.plot(xnew, smooth)


plt.show()

这篇关于在三个值之间插入曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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