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()
I’ve got the following simple script that plots a graph:
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()
As it is now, the line goes straight from point to point which looks ok, but could be better in my opinion. What I want is to smooth the line between the points. In Gnuplot I would have plotted with smooth cplines.
Is there an easy way to do this in PyPlot? I’ve found some tutorials, but they all seem rather complex.
回答 0
您可以用来scipy.interpolate.spline自己整理数据:
from scipy.interpolate import spline
# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(),300)
power_smooth = spline(T, power, xnew)
plt.plot(xnew,power_smooth)
plt.show()
scipy 0.19.0中已弃用样条线,请改用BSpline类。
从切换spline到BSpline复制并不是简单的复制/粘贴操作,需要进行一些调整:
from scipy.interpolate import make_interp_spline,BSpline# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(),300)
spl = make_interp_spline(T, power, k=3)# type: BSpline
power_smooth = spl(xnew)
plt.plot(xnew, power_smooth)
plt.show()
You could use scipy.interpolate.spline to smooth out your data yourself:
from scipy.interpolate import spline
# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(), 300)
power_smooth = spline(T, power, xnew)
plt.plot(xnew,power_smooth)
plt.show()
spline is deprecated in scipy 0.19.0, use BSpline class instead.
Switching from spline to BSpline isn’t a straightforward copy/paste and requires a little tweaking:
from scipy.interpolate import make_interp_spline, BSpline
# 300 represents number of points to make between T.min and T.max
xnew = np.linspace(T.min(), T.max(), 300)
spl = make_interp_spline(T, power, k=3) # type: BSpline
power_smooth = spl(xnew)
plt.plot(xnew, power_smooth)
plt.show()
I presume you mean curve-fitting and not anti-aliasing from the context of your question. PyPlot doesn’t have any built-in support for this, but you can easily implement some basic curve-fitting yourself, like the code seen here, or if you’re using GuiQwt it has a curve fitting module. (You could probably also steal the code from SciPy to do this as well).