【问题标题】:Plotting a smooth line with PyPlot and then set markers用 PyPlot 绘制一条平滑线,然后设置标记
【发布时间】:2013-05-23 16:08:28
【问题描述】:

假设我们有这些数据:

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 轴上表示的每个元素上绘制一个标记?有办法吗?

【问题讨论】:

    标签: python matplotlib smoothing


    【解决方案1】:

    这是你要找的吗?

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

    【讨论】:

      猜你喜欢
      • 2011-07-14
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      相关资源
      最近更新 更多