gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:36:03 25 4
gpt4 key购买 nike

假设我们有以下数据:

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

关于python - 使用 PyPlot 绘制平滑线,然后设置标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16796255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com