PyPlot 的預設線圖方法用直線連接資料點。這種方法可能會導致鋸齒狀線條,尤其是在處理大型資料集時。幸運的是,有一個簡單的方法可以使用 scipy.interpolate.spline 來平滑這些線條。
<code class="python"># Import necessary libraries import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import spline # Define data arrays 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]) # Generate a smoothed spline xnew = np.linspace(T.min(), T.max(), 300) power_smooth = spline(T, power, xnew) # Plot the smoothed line plt.plot(xnew, power_smooth) plt.show()</code>
平滑之前:
平滑後:
<code class="python"># Import make_interp_spline and BSpline from scipy.interpolate import make_interp_spline, BSpline # Generate a smoothed spline using BSpline spl = make_interp_spline(T, power, k=3) power_smooth = spl(xnew) # Plot the smoothed line plt.plot(xnew, power_smooth) plt.show()</code>
以上是如何使用 PyPlot 和 Scipy 在 Python 中平滑線圖?的詳細內容。更多資訊請關注PHP中文網其他相關文章!