pip3 安裝:
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
Linux 系統(tǒng)也可以使用 Linux 包管理器來安裝:
Debian / Ubuntu:
sudo apt-get install python-matplotlib
Fedora / Redhat:sudo yum install python-matplotlib
安裝完后,你可以使用 python -m pip list 命令來查看是否安裝了 matplotlib 模塊。
$ pip3 list | grep matplotlib matplotlib 3.3.0
import numpy as np from matplotlib import pyplot as plt x = np.arange(1,11) y = 2 * x + 5 plt.title("Matplotlib demo") plt.xlabel("x axis caption") plt.ylabel("y axis caption") plt.plot(x,y) plt.show()
以上實例中,np.arange() 函數(shù)創(chuàng)建 x 軸上的值。y 軸上的對應(yīng)值存儲在另一個數(shù)組對象 y 中。 這些值使用 matplotlib 軟件包的 pyplot 子模塊的 plot() 函數(shù)繪制。
圖形由 show() 函數(shù)顯示。
Matplotlib 默認(rèn)情況不支持中文,我們可以使用以下簡單的方法來解決。
這里我們使用思源黑體,思源黑體是 Adobe 與 Google 推出的一款開源字體。
官網(wǎng): https://source.typekit.com/source-han-serif/cn/
GitHub 地址: https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese
打開鏈接后,在里面選一個就好了:
可以下載個 OTF 字體,比如 SourceHanSansSC-Bold.otf,將該文件文件放在當(dāng)前執(zhí)行的代碼文件中:
SourceHanSansSC-Bold.otf 文件放在當(dāng)前執(zhí)行的代碼文件中:
import numpy as np from matplotlib import pyplot as plt import matplotlib # fname 為 你下載的字體庫路徑,注意 SourceHanSansSC-Bold.otf 字體的路徑 zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf") x = np.arange(1,11) y = 2 * x + 5 plt.title("立地貨 - 測試", fontproperties=zhfont1) # fontproperties 設(shè)置中文顯示,fontsize 設(shè)置字體大小 plt.xlabel("x 軸", fontproperties=zhfont1) plt.ylabel("y 軸", fontproperties=zhfont1) plt.plot(x,y) plt.show()
此外,我們還可以使用系統(tǒng)的字體:
from matplotlib import pyplot as plt import matplotlib a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist]) for i in a: print(i)
打印出你的 font_manager 的 ttflist 中所有注冊的名字,找一個看中文字體例如:STFangsong(仿宋),然后添加以下代碼即可:
plt.rcParams['font.family']=['STFangsong']
作為線性圖的代替,可以通過向 plot() 函數(shù)添加格式字符串來顯示離散值。 可以使用以下格式化字符。
字符 | 描述 |
'-' | 實線樣式 |
'--' | 短橫線樣式 |
'-.' | 點劃線樣式 |
':' | 虛線樣式 |
'.' | 點標(biāo)記 |
',' | 像素標(biāo)記 |
'o' | 圓標(biāo)記 |
'v' | 倒三角標(biāo)記 |
'^' | 正三角標(biāo)記 |
'<' | 左三角標(biāo)記 |
'>' | 右三角標(biāo)記 |
'1' | 下箭頭標(biāo)記 |
'2' | 上箭頭標(biāo)記 |
'3' | 左箭頭標(biāo)記 |
'4' | 右箭頭標(biāo)記 |
's' | 正方形標(biāo)記 |
'p' | 五邊形標(biāo)記 |
'*' | 星形標(biāo)記 |
'h' | 六邊形標(biāo)記 1 |
'H' | 六邊形標(biāo)記 2 |
'+' | 加號標(biāo)記 |
'x' | X 標(biāo)記 |
'D' | 菱形標(biāo)記 |
'd' | 窄菱形標(biāo)記 |
'|' | 豎直線標(biāo)記 |
'_' | 水平線標(biāo)記 |
以下是顏色的縮寫:
字符 | 顏色 |
'b' | 藍色 |
'g' | 綠色 |
'r' | 紅色 |
'c' | 青色 |
'm' | 品紅色 |
'y' | 黃色 |
'k' | 黑色 |
'w' | 白色 |
要顯示圓來代表點,而不是上面示例中的線,請使用 ob 作為 plot() 函數(shù)中的格式字符串。
import numpy as np from matplotlib import pyplot as plt x = np.arange(1,11) y = 2 * x + 5 plt.title("Matplotlib demo") plt.xlabel("x axis caption") plt.ylabel("y axis caption") plt.plot(x,y,"ob") plt.show()
執(zhí)行輸出結(jié)果如下圖:
以下實例使用 matplotlib 生成正弦波圖。
import numpy as np import matplotlib.pyplot as plt # 計算正弦曲線上點的 x 和 y 坐標(biāo) x = np.arange(0, 3 * np.pi, 0.1) y = np.sin(x) plt.title("sine wave form") # 使用 matplotlib 來繪制點 plt.plot(x, y) plt.show()
執(zhí)行輸出結(jié)果如下圖:
subplot() 函數(shù)允許你在同一圖中繪制不同的東西。
以下實例繪制正弦和余弦值:
import numpy as np import matplotlib.pyplot as plt # 計算正弦和余弦曲線上的點的 x 和 y 坐標(biāo) x = np.arange(0, 3 * np.pi, 0.1) y_sin = np.sin(x) y_cos = np.cos(x) # 建立 subplot 網(wǎng)格,高為 2,寬為 1 # 激活第一個 subplot plt.subplot(2, 1, 1) # 繪制第一個圖像 plt.plot(x, y_sin) plt.title('Sine') # 將第二個 subplot 激活,并繪制第二個圖像 plt.subplot(2, 1, 2) plt.plot(x, y_cos) plt.title('Cosine') # 展示圖像 plt.show()
執(zhí)行輸出結(jié)果如下圖:
pyplot 子模塊提供 bar() 函數(shù)來生成條形圖。
以下實例生成兩組 x 和 y 數(shù)組的條形圖。
from matplotlib import pyplot as plt x = [5,8,10] y = [12,16,6] x2 = [6,9,11] y2 = [6,15,7] plt.bar(x, y, align = 'center') plt.bar(x2, y2, color = 'g', align = 'center') plt.title('Bar graph') plt.ylabel('Y axis') plt.xlabel('X axis') plt.show()
執(zhí)行輸出結(jié)果如下圖:
numpy.histogram() 函數(shù)是數(shù)據(jù)的頻率分布的圖形表示。 水平尺寸相等的矩形對應(yīng)于類間隔,稱為 bin,變量 height 對應(yīng)于頻率。
numpy.histogram()函數(shù)將輸入數(shù)組和 bin 作為兩個參數(shù)。 bin 數(shù)組中的連續(xù)元素用作每個 bin 的邊界。
import numpy as np a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) np.histogram(a,bins = [0,20,40,60,80,100]) hist,bins = np.histogram(a,bins = [0,20,40,60,80,100]) print (hist) print (bins)
輸出結(jié)果為:
[3 4 5 2 1] [ 0 20 40 60 80 100]
Matplotlib 可以將直方圖的數(shù)字表示轉(zhuǎn)換為圖形。 pyplot 子模塊的 plt() 函數(shù)將包含數(shù)據(jù)和 bin 數(shù)組的數(shù)組作為參數(shù),并轉(zhuǎn)換為直方圖。
from matplotlib import pyplot as plt import numpy as np a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]) plt.hist(a, bins = [0,20,40,60,80,100]) plt.title("histogram") plt.show()
執(zhí)行輸出結(jié)果如下圖:
Matplotlib 更多參考內(nèi)容: