我希望绘制一条平滑的曲线,给出一些点(一维x和y数组)。我想我可以使用make_interp_spline
包,但看起来x
数组需要均匀排序...我如何使用make_interp_spline
而不对数组的x维排序或解决错误?
from scipy.interpolate import make_interp_spline
import numpy as np
# from scipy import stats
import matplotlib.pyplot as plt
x = np.array([-31,-30,-30,-32,-36,-39])
y = np.array([60,62,64,65,64,64])
# plot non-interpolated curve
plt.plot(x,y);
# x = x.reshape(6)
# y = y.reshape(6)
# wher the error occurs about the 1d sorted array
X_Y_Spline = make_interp_spline(x, y)
X_ = np.linspace(x.min(), x.max(), 500)
Y_ = X_Y_Spline(X_)
plt.plot(X_, Y_);
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_2083810/662623888.py in <module>
7 #y = y.reshape(6)
8
----> 9 X_Y_Spline = make_interp_spline(x, y)
10
11 X_ = np.linspace(x.min(), x.max(), 500)
~/miniconda3/envs/py3_std_maps/lib/python3.9/site-packages/scipy/interpolate/_bsplines.py in make_interp_spline(x, y, k, t, bc_type, axis, check_finite)
784
785 if x.ndim != 1 or np.any(x[1:] < x[:-1]):
--> 786 raise ValueError("Expect x to be a 1-D sorted array_like.")
787 if np.any(x[1:] == x[:-1]):
788 raise ValueError("Expect x to not have duplicates")
ValueError: Expect x to be a 1-D sorted array_like.
3条答案
按热度按时间8i9zcol21#
最好使用“scipy.interpolate”中的“interp1d”。
h9vpoimq2#
解决方法是在
x
和y
数组之后建立单调递增的n
维度。请参阅:
zdwk9cvp3#
只需对x数组进行np.argsort,并使用结果同时对x和y进行排序。