在scipy.interplot Python中绘制错误

时间:2017-09-05 20:03:09

标签: python matplotlib plot

我正在尝试使用以下代码绘制一条平滑线:

from scipy.interpolate import spline
import matplotlib.pyplot as plt
import numpy as np
import collections
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

t1 = np.arange(1, 35, 1)
t2 = [479772,479772,479772,479772,479772,479772,479772,479772,
     479772,479772,479772,479772,479772,479772,479772,479772,479772,
     479772,479772,479772,479772,479772,479772,479819,479819,479819,
     479819,479819,479819,479878,479878,479878,479878,479878]

P = t2
T = t1

xnew = np.linspace(T.min(),T.max(),33)

P_smooth = spline(T,P,xnew)

fig = plt.figure()
ax = fig.add_subplot(111)
fig.set_size_inches(7, 4, forward=True)
plt.ticklabel_format(useOffset=False)
plt.plot(xnew,P_smooth, '--k')
plt.xlabel('$w$')
plt.ylabel('z')
plt.show()

正如您可能注意到的那样,当情节中w为23和29时,z似乎有所减少。但是,正如您在t2列表中看到的那样,任何地方都没有减少。有任何建议可以纠正这个问题吗?

1 个答案:

答案 0 :(得分:1)

您正在使用三次样条插值方法。这可能会导致曲线在提供的点下方运行。另一种方法是使用interp1d代替线性样条插值:

from scipy.interpolate import interp1d
...
f = interp1d(t1, t2)
...
plt.plot(t1, t2, 'o', xnew, f(xnew), '-')

结果图:

interp1d