matplotlib绘制平滑的曲线

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

1.曲线拟合

使用scipy库可以拟合曲线.
没拟合的图:

import matplotlib.pyplot as plt
import numpy as np
T = 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()

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

import matplotlib.pyplot as plt
import numpy as np
T = 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 spline
xnew = np.linspace(T.min(),T.max(),300) #300 represents number of points to make between T.min and T.max
power_smooth = spline(T,power,xnew)
plt.plot(xnew,power_smooth)
plt.show()

2

2.间隔画曲线

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

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

3
间隔点画图:

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

4
参考资料:
Plot smooth line with PyPlot
matplotlib中的平滑线图具有较少的数据点

猜你喜欢

转载自blog.csdn.net/zhangpeterx/article/details/83684239