Python 數(shù)據(jù)科學(xué) Matplotlib圖庫(kù)詳解
Matplotlib 是 Python 的二維繪圖庫(kù),用于生成符合出版質(zhì)量或跨平臺(tái)交互環(huán)境的各類圖形。
圖形解析與工作流
圖形解析

工作流
Matplotlib 繪圖的基本步驟:
1 準(zhǔn)備數(shù)據(jù)
2 創(chuàng)建圖形
3 繪圖
4 自定義設(shè)置
5 保存圖形
6 顯示圖形
import matplotlib.pyplot as plt
x = [1,2,3,4] # step1
y = [10,20,25,30]
fig = plt.figure() # step2
ax = fig.add_subplot(111) # step3
ax.plot(x, y, color='lightblue', linewidth=3) # step3\4
ax.scatter([2,4,6],
[5,15,25],
color='darkgreen',
marker='^')
ax.set_xlim(1, 6.5)
plt.savefig('foo.png') # step5
plt.show() # step6
準(zhǔn)備數(shù)據(jù)
一維數(shù)據(jù)
import numpy as np x = np.linspace(0, 10, 100) y = np.cos(x) z = np.sin(x)
二維數(shù)據(jù)或圖片
data = 2 * np.random.random((10, 10))
data2 = 3 * np.random.random((10, 10))
Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
from matplotlib.cbook import get_sample_data
img = np.load('E:/anaconda3/envs/torch/Lib/site-packages/matplotlib/mpl-data/aapl.npz')
繪制圖形
import matplotlib.pyplot as plt
畫(huà)布
fig = plt.figure() fig2 = plt.figure(figsize=plt.figaspect(2.0))
坐標(biāo)軸
圖形是以坐標(biāo)軸為核心繪制的,大多數(shù)情況下,子圖就可以滿足需求。子圖是柵格系統(tǒng)的坐標(biāo)軸。
fig.add_axes() ax1 = fig.add_subplot(221) # row-col-num ax3 = fig.add_subplot(212) fig3, axes = plt.subplots(nrows=2,ncols=2) fig4, axes2 = plt.subplots(ncols=3)


繪圖例程
一維數(shù)據(jù)
fig, ax = plt.subplots() lines = ax.plot(x,y) # 用線或標(biāo)記連接點(diǎn) ax.scatter(x,y) # 縮放或著色未連接的點(diǎn) axes[0,0].bar([1,2,3],[3,4,5]) # 繪制等寬縱向矩形 axes[1,0].barh([0.5,1,2.5],[0,1,2]) # 繪制等高橫向矩形 axes[1,1].axhline(0.45) # 繪制與軸平行的橫線 axes[0,1].axvline(0.65) # 繪制與軸垂直的豎線 ax.fill(x,y,color='blue') # 繪制填充多邊形 ax.fill_between(x,y,color='yellow') # 填充y值和0之間

二維數(shù)據(jù)或圖片
import matplotlib.image as imgplt
img = imgplt.imread('C:/Users/Administrator/Desktop/timg.jpg')
fig, ax = plt.subplots()
im = ax.imshow(img, cmap='gist_earth', interpolation='nearest', vmin=-200, vmax=200)# 色彩表或RGB數(shù)組
axes2[0].pcolor(data2) # 二維數(shù)組偽彩色圖
axes2[0].pcolormesh(data) # 二維數(shù)組等高線偽彩色圖
CS = plt.contour(Y,X,U) # 等高線圖
axes2[2].contourf(data)
axes2[2]= ax.clabel(CS) # 等高線圖標(biāo)簽

向量場(chǎng)
axes[0,1].arrow(0,0,0.5,0.5) # 為坐標(biāo)軸添加箭頭 axes[1,1].quiver(y,z) # 二維箭頭 axes[0,1].streamplot(X,Y,U,V) # 二維箭頭
數(shù)據(jù)分布
ax1.hist(y) # 直方圖 ax3.boxplot(y) # 箱形圖 ax3.violinplot(z) # 小提琴圖
自定義圖形 顏色、色條與色彩表
plt.plot(x, x, x, x**2, x, x**3)
ax.plot(x, y, alpha = 0.4)
ax.plot(x, y, c='k')
fig.colorbar(im, orientation='horizontal')
im = ax.imshow(img,
cmap='seismic')

標(biāo)記
fig, ax = plt.subplots() ax.scatter(x,y,marker=".") ax.plot(x,y,marker="o")

線型
plt.plot(x,y,linewidth=4.0) plt.plot(x,y,ls='solid') plt.plot(x,y,ls='--') plt.plot(x,y,'--',x**2,y**2,'-.') plt.setp(lines,color='r',linewidth=4.0)

文本與標(biāo)注
ax.text(1,
-2.1,
'Example Graph',
style='italic')
ax.annotate("Sine",
xy=(8, 0),
xycoords='data',
xytext=(10.5, 0),
textcoords='data',
arrowprops=dict(arrowstyle="->",
connectionstyle="arc3"),)
數(shù)學(xué)符號(hào)
plt.title(r'$sigma_i=15$', fontsize=20)
尺寸限制、圖例和布局
尺寸限制與自動(dòng)調(diào)整
ax.margins(x=0.0,y=0.1) # 添加內(nèi)邊距
ax.axis('equal') # 將圖形縱橫比設(shè)置為1
ax.set(xlim=[0,10.5],ylim=[-1.5,1.5]) # 設(shè)置x軸與y軸的限
ax.set_xlim(0,10.5)
圖例
ax.set(title='An Example Axes',
ylabel='Y-Axis',
xlabel='X-Axis') # 設(shè)置標(biāo)題與x、y軸的標(biāo)簽
ax.legend(loc='best') # 自動(dòng)選擇最佳的圖例位置
標(biāo)記
ax.xaxis.set(ticks=range(1,5),
ticklabels=[3,100,-12,"foo"]) # 手動(dòng)設(shè)置X軸刻度
ax.tick_params(axis='y',
direction='inout',
length=10) # 設(shè)置Y軸長(zhǎng)度與方向
子圖間距
fig3.subplots_adjust(wspace=0.5,
hspace=0.3,
left=0.125,
right=0.9,
top=0.9,
bottom=0.1)
fig.tight_layout() # 設(shè)置畫(huà)布的子圖布局
坐標(biāo)軸邊線
ax1.spines['top'].set_visible(False) # 隱藏頂部坐標(biāo)軸線
ax1.spines['bottom'].set_position(('outward',10)) # 設(shè)置底部邊線的位置為outward
保存
#保存畫(huà)布
plt.savefig('foo.png')
# 保存透明畫(huà)布
plt.savefig('foo.png', transparent=True)
顯示圖形
plt.show()
關(guān)閉與清除
plt.cla() # 清除坐標(biāo)軸 plt.clf() # 清除畫(huà)布 plt.close() # 關(guān)閉窗口
以上就是Python 數(shù)據(jù)科學(xué) Matplotlib的詳細(xì)內(nèi)容,更多關(guān)于Python 數(shù)據(jù)科學(xué) Matplotlib的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 掌握python polars庫(kù)進(jìn)行高效高速的數(shù)據(jù)處理。
- python Polars庫(kù)的使用簡(jiǎn)介
- Python mlxtend庫(kù)數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)補(bǔ)充工具功能探索
- 盤點(diǎn)20個(gè)Python數(shù)據(jù)科學(xué)庫(kù)神器打造數(shù)據(jù)魔法世界
- Python?Prometheus接口揭秘?cái)?shù)據(jù)科學(xué)新技巧
- 3個(gè)用于數(shù)據(jù)科學(xué)的頂級(jí)Python庫(kù)
- Python進(jìn)行數(shù)據(jù)科學(xué)工作的簡(jiǎn)單入門教程
- python polars數(shù)據(jù)科學(xué)庫(kù)對(duì)比Pandas優(yōu)勢(shì)分析
相關(guān)文章
mat矩陣和npy矩陣實(shí)現(xiàn)互相轉(zhuǎn)換(python和matlab)
這篇文章主要介紹了mat矩陣和npy矩陣實(shí)現(xiàn)互相轉(zhuǎn)換(python和matlab),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Python開(kāi)發(fā)時(shí)報(bào)TypeError:?‘int‘?object?is?not?iterable錯(cuò)誤的解決方
Python寫(xiě)循環(huán)程序的時(shí)候遇到TypeError:'int'object is not iterable,所以下面這篇文章主要給大家介紹了關(guān)于Python開(kāi)發(fā)時(shí)報(bào)TypeError:'int'?object?is?not?iterable錯(cuò)誤的解決方式,需要的朋友可以參考下2022-06-06
python list 查詢是否存在并且并返回下標(biāo)的操作
這篇文章主要介紹了python list 查詢是否存在并且并返回下標(biāo)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
對(duì)Python中內(nèi)置異常層次結(jié)構(gòu)詳解
今天小編就為大家分享一篇對(duì)Python中內(nèi)置異常層次結(jié)構(gòu)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
python tkinter中的錨點(diǎn)(anchor)問(wèn)題及處理
這篇文章主要介紹了python tkinter中的錨點(diǎn)(anchor)問(wèn)題及處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Python?Nuitka打包的實(shí)現(xiàn)步驟
在Python應(yīng)用程序開(kāi)發(fā)中,打包是將代碼和依賴項(xiàng)組合成可執(zhí)行文件或庫(kù)的關(guān)鍵步驟之一,本文主要介紹了Python?Nuitka打包的實(shí)現(xiàn)步驟,感興趣的可以了解一下2023-12-12
解決Django migrate不能發(fā)現(xiàn)app.models的表問(wèn)題
今天小編就為大家分享一篇解決Django migrate不能發(fā)現(xiàn)app.models的表問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python新手必讀bytearray對(duì)象使用技巧掌握
Python中的bytearray是一個(gè)可變序列,通常用于存儲(chǔ)二進(jìn)制數(shù)據(jù),它允許在不創(chuàng)建新的對(duì)象的情況下就地修改數(shù)據(jù),非常適用于處理字節(jié)數(shù)據(jù),本文將深入學(xué)習(xí)bytearray對(duì)象的使用,包括創(chuàng)建、修改、切片和常見(jiàn)應(yīng)用場(chǎng)景2023-12-12

