1. 程式人生 > 其它 >Task01:Matplotlib初相識

Task01:Matplotlib初相識

一、明晰繪製一張圖的組成條件

Figure:最基本的一級
Axes:在Figure上建立子圖的容器(如果Figure中僅含一子圖,則該容器可省略)
Axis:用於處理子圖上和座標軸和網格相關的元素
Tick:用於處理和刻度相關的元素

二、兩種繪圖模式模板

A.OO模式:更能實現高階繪圖的需求

# step1 準備資料
x = np.linspace(0, 2, 100)
y = x**2

# step2 第五章:設定繪圖樣式(非必須)
mpl.rc('lines', winewidth=4, linestyle='-.')

# step3 第三章:定義佈局
fig, ax = plt.subplots()

# step4 第二章:繪製圖像
ax.plot(x, y, label="linear")

# step5 第四章:標籤,文字和圖例
ax.set_xlabel('x label') 
ax.set_ylabel('y label') 
ax.set_title("Simple Plot")  
ax.legend()
plt.show()

B.pyplot模式:方便簡潔,容易上手

# step1 準備資料
x = np.linspace(0, 2, 100)
y = x**2

# step2 第五章:設定繪圖樣式(非必須)
mpl.rc('lines', winewidth=4, linestyle='-.')

# step3 第二章:繪製圖像
plt.plot(x, y, label="linear")

# step4 第五章:標籤,文字和圖例
plt.xlabel('x label')
plt.ylabel('y label')
plt.title('Simple Plot')
plt.legend()
plt.show()