如何使用 matplotlib 繪製平滑線?
要使用 matplotlib 繪製平滑線,我們可以採取以下步驟 -
步驟
設定圖形大小並調整子影像之間和周圍的邊距。
建立資料點列表,x 和 y。
繪製 x 和 y 資料點。
為平滑線建立 x_new 和 bspline 資料點。
獲取 y_new 資料點。計算插值 B 樣條的(係數)。
使用 plot() 方法繪製 x_new 和 y_new 資料點。
要顯示圖形,請使用 show() 方法。
示例
import numpy as np from matplotlib import pyplot as plt from scipy import interpolate # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # x and y data points x = np.array([1, 3, 4, 6, 7]) y = np.array([5, 1, 3, 2, 4]) # Plot the data points plt.plot(x, y) # x_new, bspline, y_new x_new = np.linspace(1, 5, 50) bspline = interpolate.make_interp_spline(x, y) y_new = bspline(x_new) # Plot the new data points plt.plot(x_new, y_new) plt.show()
輸出
它將生成以下輸出 -
廣告