設定 Matplotlib 圖/軸屬性的首選方式是什麼?
要設定繪圖的屬性,我們可以獲取繪圖的當前軸。然後,我們可以執行多個 set_* 方法以設定繪圖的屬性。
步驟
使用 subplots() 方法建立圖形和一組子圖,其中 figsize=(5, 5)。
使用 numpy 建立 x 和 y 資料點。
使用 plot() 方法繪製 x 和 y。
使用 set_xlabel() 和 set_ylabel() 方法設定標題和標籤(用於 X 和 Y 軸)。
要顯示圖形,請使用 show() 方法。
示例
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() x = np.linspace(-1, 1, 10) y = np.exp(x) ax.plot(x, y, c='red') ax.set_title('y=exp(x)') ax.set_xlabel('x') ax.set_ylabel('y') plt.show()
輸出
廣告