失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > matplotlib绘制平滑的曲线

matplotlib绘制平滑的曲线

时间:2023-06-07 02:01:40

相关推荐

matplotlib绘制平滑的曲线

matplotlib绘制平滑的曲线有2种常用的方法

1.曲线拟合

使用scipy库可以拟合曲线.

没拟合的图:

import matplotlib.pyplot as pltimport numpy as npT = np.array([6, 7, 8, 9, 10, 11, 12])power = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00])plt.plot(T,power)plt.show()

使用scipy.interpolate.spline拟合曲线:

import matplotlib.pyplot as pltimport numpy as npT = np.array([6, 7, 8, 9, 10, 11, 12])power = np.array([1.53E+03, 5.92E+02, 2.04E+02, 7.24E+01, 2.72E+01, 1.10E+01, 4.70E+00])from scipy.interpolate import splinexnew = np.linspace(T.min(),T.max(),300) #300 represents number of points to make between T.min and T.maxpower_smooth = spline(T,power,xnew)plt.plot(xnew,power_smooth)plt.show()

2.间隔画曲线

当matplotlib画图的横坐标为时间时,使用scipy.interpolate.spline拟合曲线就会遇到麻烦,我们可以采用间隔点画图。

原先的图:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(0,10,0.2)y = np.random.rand(len(x))plt.plot(x,y, marker="o")plt.show()

间隔点画图:

import matplotlib.pyplot as pltimport numpy as npx = np.arange(0,10,0.2)y = np.random.rand(len(x))plt.plot(x[::4],y[::4], marker="o")plt.show()

参考资料:

Plot smooth line with PyPlot

matplotlib中的平滑线图具有较少的数据点

如果觉得《matplotlib绘制平滑的曲线》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。