使用PyPlot绘制平滑线,然后设置标记

时间:2013-05-28 15:46:33

标签: python matplotlib smoothing

说我们有这些数据:

import matplotlib.pyplot as plt
import numpy as np

Y = np.array([500.,  1000.,  1500.,  2000.,  2500.,  3000.,  3500.,  4000.,
        4500.,  5000.,  5500.,  6000.,  6500.,  7000.,  7500.,  8000.,
        8500.,  8999.])
P = np.array([0.35719454,  1.33640227,  3.6250187 ,  4.6098672 ,  3.07393609,
        3.37146857,  3.63601765,  4.22452476,  3.89485839,  4.1829315 ,
        4.02657142,  4.17620968,  4.06966698,  4.18009466,  4.16220121,
        4.60149918,  4.31294132,  3.94933813])

plt.plot(Y,P)
plt.show()

我怎样才能平滑它并同时在x轴上表示的每个元素上绘制标记?有办法吗?

1 个答案:

答案 0 :(得分:3)

这是你在找什么?

import matplotlib.pyplot as plt
import numpy as np

Y = np.array([500.,  1000.,  1500.,  2000.,  2500.,  3000.,  3500.,  4000.,
        4500.,  5000.,  5500.,  6000.,  6500.,  7000.,  7500.,  8000.,
        8500.,  8999.])
P = np.array([0.35719454,  1.33640227,  3.6250187 ,  4.6098672 ,  3.07393609,
        3.37146857,  3.63601765,  4.22452476,  3.89485839,  4.1829315 ,
        4.02657142,  4.17620968,  4.06966698,  4.18009466,  4.16220121,
        4.60149918,  4.31294132,  3.94933813])

from scipy.interpolate import interp1d
f2 = interp1d(Y, P, kind='cubic')

xnew = np.linspace(min(Y), max(Y), 100) # you can change the 100 here to a smaller number if you want less 'smoothness'
plt.plot(Y,P,'o',xnew, f2(xnew),'--')

plt.show()