Matplotlib是一個綜合庫,用於在Python中創建靜態,動畫和交互式可視化。
1.1 畫線
下面介紹如何使用lot()創建帶有文本標籤的線條圖
import matplotlib # 導入matplotlib
import matplotlib.pyplot as plt # 導入matplotlib的畫圖函數
import numpy as np # 導入爲畫圖做數據準備的庫
# Data for plotting ——> 畫圖的數據
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
# fig是圖片,每一個Axes(ax)對象都是一個擁有自己座標系統的繪圖區域
ax.plot(t, s) # 繪圖
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title='About as simple as it gets, folks') # 設置橫縱軸標籤與圖像標題
ax.grid() # 設置網格
fig.savefig("test.png") # 保存圖像爲圖片,圖片格式爲爲png格式
plt.show() # 顯示所繪製的圖像