ValueError:期望 x 是一维排序的 array_like。我正在尝试绘制平滑曲线,但无法

RIS*_*GAR 8 python matplotlib scipy

在 python 中进行一些计算后,我得到了两个数组。

第一 :

**t**: [0, 1.5e-13, 3e-13, 4.5e-13, 6e-13, 7.499999999999999e-13, 8.999999999999999e-13, 1.05e-12, 1.2e-12, 1.35e-12, 1.4999999999999999e-12, 1.6499999999999998e-12, 3e-13, 4.5e-13, 6e-13, 7.499999999999999e-13, 8.999999999999999e-13, 1.05e-12, 1.2e-12, 1.35e-12, 1.4999999999999999e-12]
Run Code Online (Sandbox Code Playgroud)

第二个:

**X2**: [2.0000000000000003e-34, 3.953299280814115e-14, -0.16661594736661114, 363384313676.0453, 1.6249307273647528e+24, -2.606395610181476e+37, 1.9393976227227167e+50, -1.0229289218046666e+63, 3.6974904635770745e+75, -3.685245806003695e+87, -7.685163462952308e+100, 8.267305810721622e+113, -0.16661594736661114, 363384313676.0453, 1.6249307273647528e+24, -2.606395610181476e+37, 1.9393976227227167e+50, -1.0229289218046666e+63, 3.6974904635770745e+75, -3.685245806003695e+87, -7.685163462952308e+100]
Run Code Online (Sandbox Code Playgroud)

我的代码:

     from matplotlib import pyplot as plt
     from scipy.interpolate import make_interp_spline as sp, BSpline
     x_new = np.linspace(min(t), max(t), 300)
     spl = sp(t, X2, k=3)  
     a_BSpline = sp(t, X2)
     y_new = spl(x_new)

     plt.plot(x_new,y_new)
     plt.show()
Run Code Online (Sandbox Code Playgroud)

我收到错误为

ValueError: Expect x to be a 1-D sorted array_like.
Run Code Online (Sandbox Code Playgroud)

Jon*_*nas 5

正如所说ValueError,你需要两个表达式才为真:1-Dsorted。对应于:

if x.ndim != 1 or np.any(x[1:] <= x[:-1]):
         raise ValueError("Expect x to be a 1-D sorted array_like.")
Run Code Online (Sandbox Code Playgroud)

如果你检查这个:

t = np.array([0, 1.5e-13, 3e-13, 4.5e-13, 6e-13, 7.499999999999999e-13, 8.999999999999999e-13, 1.05e-12, 1.2e-12, 1.35e-12, 1.4999999999999999e-12, 1.6499999999999998e-12, 3e-13, 4.5e-13, 6e-13, 7.499999999999999e-13, 8.999999999999999e-13, 1.05e-12, 1.2e-12, 1.35e-12, 1.4999999999999999e-12])

print(t.ndim)
print(np.any(t[1:] <= t[:-1]))
Run Code Online (Sandbox Code Playgroud)

输出将是:

1
True
Run Code Online (Sandbox Code Playgroud)

这意味着您的数组是一维的,但未排序。

你的问题是,即使你对数组进行排序np.sort(),错误仍然会上升,因为你的数组中的数字是相等的。因此<=测试仍然是正确的。

我会建议format()您的数字,以便它们变得可区分。