簡體   English   中英

使用PyPlot繪制平滑線,然后設置標記

[英]Plotting a smooth line with PyPlot and then set markers

假設我們有這些數據:

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軸上表示的每個元素上繪制標記? 有辦法嗎?

這是你想要的?

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM