【问题标题】:Scipy to make the graph smootherScipy 使图形更平滑
【发布时间】:2016-02-29 14:27:11
【问题描述】:

我尝试使用 scipy 使我的图表更平滑,但结果是一条水平线。

代码:

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

dd = [1.0, 0.7071067811865476, 0.5, 0.3535533905932738, 0.25, 0.1767766952966369]
y = range(0,6)

dd1 = np.array(dd)
y1 = np.array(y)

xsmooth = np.linspace (dd1.max(),dd1.min(),5)
ysmooth = spline (dd1,y1,xsmooth)

plt.plot(xsmooth ,ysmooth)
plt.show()

提前致谢

【问题讨论】:

  • 你的代码没有运行:ValueError: shapes (8,6) and (5,) not aligned: 6 (dim 1) != 5 (dim 0).
  • @SiHa yes.thanks SiHa.

标签: python-2.7 matplotlib plot scipy spline


【解决方案1】:

我认为您可能在代码中混淆了 x 和 y。 如果 dd 是你的 y 值并且 y 是你的 x 值,你可以像这样执行样条插值:

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

y = [1.0, 0.7071067811865476, 0.5, 0.3535533905932738, 0.25, 0.1767766952966369]
x = range(0,6)

y = np.array(y)
x = np.array(x)

xsmooth = np.linspace (x.min(),x.max(),20) # an x vector with more intermediate values
ysmooth = spline(x,y,xsmooth) 

plt.plot(xsmooth ,ysmooth)
plt.plot(x,y,'o')
plt.show()

【讨论】:

    猜你喜欢
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 2021-11-28
    • 2021-03-07
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多