matplotlib 是 python 的二維繪圖庫,用于生成符合出版質量或跨平臺交互環境的各類圖形。
圖形解析與工作流
圖形解析
工作流
matplotlib 繪圖的基本步驟:
1 準備數據
2 創建圖形
3 繪圖
4 自定義設置
5 保存圖形
6 顯示圖形
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 |
準備數據
一維數據
1
2
3
4
5
|
import numpy as np x = np.linspace( 0 , 10 , 100 ) y = np.cos(x) z = np.sin(x) |
二維數據或圖片
1
2
3
4
5
6
7
|
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' ) |
繪制圖形
1
|
import matplotlib.pyplot as plt |
畫布
1
2
|
fig = plt.figure() fig2 = plt.figure(figsize = plt.figaspect( 2.0 )) |
坐標軸
圖形是以坐標軸為核心繪制的,大多數情況下,子圖就可以滿足需求。子圖是柵格系統的坐標軸。
1
2
3
4
5
|
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 ) |
繪圖例程
一維數據
1
2
3
4
5
6
7
8
9
|
fig, ax = plt.subplots() lines = ax.plot(x,y) # 用線或標記連接點 ax.scatter(x,y) # 縮放或著色未連接的點 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之間 |
二維數據或圖片
1
2
3
4
5
6
7
8
9
10
11
|
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數組 axes2[ 0 ].pcolor(data2) # 二維數組偽彩色圖 axes2[ 0 ].pcolormesh(data) # 二維數組等高線偽彩色圖 cs = plt.contour(y,x,u) # 等高線圖 axes2[ 2 ].contourf(data) axes2[ 2 ] = ax.clabel(cs) # 等高線圖標簽 |
向量場
1
2
3
|
axes[ 0 , 1 ].arrow( 0 , 0 , 0.5 , 0.5 ) # 為坐標軸添加箭頭 axes[ 1 , 1 ].quiver(y,z) # 二維箭頭 axes[ 0 , 1 ].streamplot(x,y,u,v) # 二維箭頭 |
數據分布
1
2
3
|
ax1.hist(y) # 直方圖 ax3.boxplot(y) # 箱形圖 ax3.violinplot(z) # 小提琴圖 |
自定義圖形 顏色、色條與色彩表
1
2
3
4
5
6
|
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' ) |
標記
1
2
3
|
fig, ax = plt.subplots() ax.scatter(x,y,marker = "." ) ax.plot(x,y,marker = "o" ) |
線型
1
2
3
4
5
|
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 ) |
文本與標注
1
2
3
4
5
6
7
8
9
10
11
|
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" ),) |
數學符號
1
|
plt.title(r '$sigma_i=15$' , fontsize = 20 ) |
尺寸限制、圖例和布局
尺寸限制與自動調整
1
2
3
4
|
ax.margins(x = 0.0 ,y = 0.1 ) # 添加內邊距 ax.axis( 'equal' ) # 將圖形縱橫比設置為1 ax. set (xlim = [ 0 , 10.5 ],ylim = [ - 1.5 , 1.5 ]) # 設置x軸與y軸的限 ax.set_xlim( 0 , 10.5 ) |
圖例
1
2
3
4
|
ax. set (title = 'an example axes' , ylabel = 'y-axis' , xlabel = 'x-axis' ) # 設置標題與x、y軸的標簽 ax.legend(loc = 'best' ) # 自動選擇最佳的圖例位置 |
標記
1
2
3
4
5
|
ax.xaxis. set (ticks = range ( 1 , 5 ), ticklabels = [ 3 , 100 , - 12 , "foo" ]) # 手動設置x軸刻度 ax.tick_params(axis = 'y' , direction = 'inout' , length = 10 ) # 設置y軸長度與方向 |
子圖間距
1
2
3
4
5
6
7
|
fig3.subplots_adjust(wspace = 0.5 , hspace = 0.3 , left = 0.125 , right = 0.9 , top = 0.9 , bottom = 0.1 ) fig.tight_layout() # 設置畫布的子圖布局 |
坐標軸邊線
1
2
|
ax1.spines[ 'top' ].set_visible(false) # 隱藏頂部坐標軸線 ax1.spines[ 'bottom' ].set_position(( 'outward' , 10 )) # 設置底部邊線的位置為outward |
保存
1
2
3
4
|
#保存畫布 plt.savefig( 'foo.png' ) # 保存透明畫布 plt.savefig( 'foo.png' , transparent = true) |
顯示圖形
1
|
plt.show() |
關閉與清除
1
2
3
|
plt.cla() # 清除坐標軸 plt.clf() # 清除畫布 plt.close() # 關閉窗口 |
以上就是python 數據科學 matplotlib的詳細內容,更多關于python 數據科學 matplotlib的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/weixin_44593046/article/details/118529404