【问题标题】:Plotting interpolation in python在python中绘制插值
【发布时间】:2021-02-18 13:46:41
【问题描述】:

我使用 Pandas 包将 excel 导入 python - 这非常有效。 excel有两列-一列是“x”值,而第二列是“y”值-我想绘制三次样条插值,但它不像我想象的那样工作:

df = pd.read_excel('test.xls')
f = interp1d(df['x'],df['y '])
plt.plot(f)

值在哪里:

x.          y
4,637424    0,00524
4,9027      0,014685
5,082711    0,059966
5,153686    0,124667
5,212604    0,188868
5,227324    0,250883
5,270058    0,319341
5,304338    0,406323
5,315178    0,461895
5,343454    0,82056
5,341876    0,527936
5,4028      0,865394
5,725879    0,989496
5,870601    0,99345
6,497925    0,99956
6,884001    1,00

y 值是概率。

【问题讨论】:

  • 这是一个非常琐碎的问题,可以通过重复最简单的interp1d 示例来解决。

标签: python pandas matplotlib


【解决方案1】:

in the documentation 所示,interp1d 返回一个函数,您可以使用该函数根据未知的 x 值计算 y 值。您不能绘制函数。您必须选择所需的 x 值范围,并使用它来计算 y 值,然后将这些值绘制在一起:

f = interp1d(df['x.'], df['y'])
new_x = np.linspace(df['x.'].min(), df['x.'].max(), 100)
new_y = f(new_x)

fig, ax = plt.subplots()
ax.plot(df['x.'], df['y'], 'ro')
ax.plot(new_x, new_y, '-')

【讨论】:

  • 谢谢!是否有可能以某种方式“平滑”地插入它,它就像一种概率函数(即没有尼克斯) - 它不一定要通过各个点,而是一种最小二乘拟合或什么?
  • 这是一个不同的问题。插值只是为您提供点之间的缺失值。你想要的是最合适的。你的数据看起来很逻辑。见这里stackoverflow.com/questions/55725139/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 2021-09-20
  • 2021-05-30
  • 2020-10-12
  • 1970-01-01
相关资源
最近更新 更多