本文記錄matlibplot常用基本操作,都是基本功能(代碼解釋了基本用法),不涉及復雜聯合操作,其中各用法詳細用法可參考官網。
對於matlibplot一些基本概念可以參考該片blog.解釋的很詳細。
1. 基本畫圖操作
##mofan_matplotlib.pyplot import matplotlib.pyplot as plt import numpy as np x = np.linspace(1,50) y = 2*x + 1 #draw the lines #plt.plot(x,y) #show to draw the figure :must call at the last #plt.show() #figure的使用 ##figure就是圖片窗口 #如畫兩張圖 y1 = x**2 #第一張圖 plt.figure() plt.plot(x,y) #第二張圖 #num指定第幾章圖, figsize指定圖的大小 #plt.figure(num=3, figsize=(8,5)) ##在一張圖放兩張線條 plt.plot(x, y1) plt.plot(x, y, color= 'red', linewidth=1.0, linestyle='-') plt.show()
Fig1:
2. 描述坐標
#轉換坐標單位:重寫坐標 new_ticks = np.linspace(-15,70,0.5) print(new_ticks) plt.xticks(new_ticks) plt.figure() #限定坐標軸范圍 plt.xlim(1,30) plt.ylim(2,30) #修改坐標軸名稱 plt.xlabel('x') plt.ylabel('y')
#將坐標換成字符標識
#利用正則做顯示
plt.yticks([50, 10, -10], ['really good','good','bad'])
Fig2:
3. 設置坐標軸位置
# gca = 'get current axis' ax = plt.gca() #把右邊的和上面的橫軸去掉 ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') #用下面的軸做x軸,左邊的軸做y軸 ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') #設置坐標軸位置 ax.spines['bottom'].set_position(('data',-1)) ax.spines['left'].set_position(('data',0))
Fig3:
4. 圖列
plt.plot(x, y1, color= 'red', linewidth=1.0, linestyle='-',label = 'y1=x^2') #legend:參數可控制(handles=, labels = , loc = 'best') plt.legend(loc = 'best')
Fig 4:
5. 對坐標軸每個刻度做處理
##把坐標軸上的標度拿出來單獨做標注 for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(12) #alpha:坐標透明度 label.set_bbox(dict(facecolor='red', edgecolor="None", alpha=0.7))
Fig 5:
6. 散點圖
import matplotlib.pyplot as plt import numpy as np n = 1024 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) # 設置點顏色 T = np.arctan2(Y,X) #s:size, c:color, plt.scatter(X,Y, s=75, c=T,alpha=0.5 ) plt.xlim((-1.5, 1.5)) plt.ylim((-1.5, 1.5)) plt.show()
Fig 6:
7. 柱狀圖
n = 12 X = np.arange(n) Y1 = (1-X/float(n)) * np.random.uniform(0.5, 1.0, n) Y2 = (1-X/float(n)) * np.random.uniform(0.5, 1.0, n) #畫柱狀圖 #facecolor:, edgecolor邊緣顏色 #+Y1,往上 plt.bar(X, +Y1,facecolor='#9999ff', edgecolor ='white') #加標准注 #把X,Y1的值成對傳給x,y,形成坐標 for x, y in zip(X, Y1): plt.text(x +0.4, y +0.05,'%0.2f'%y, ha = 'center',va = 'bottom' )
Fig 7:
8. 畫二維矩陣圖
import matplotlib.pyplot as plt import numpy as np import random #生成9個值 每個是一個格子 l = [random.random() for i in range(9)] a = np.array(l).reshape((3,3)) #print(a) #參數參考官網用法 #interpolation:是顯示效果 plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower') #添加bar plt.colorbar() plt.xticks(()) plt.yticks(()) plt.show()
Fig 8:
9. 畫3D圖
from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() #在fig里面加一個3D的axes ax = Axes3D(fig) X = np.arange(-4,4, 0.25) Y = np.arange(-4,4, 0.25) #把X,Ymesh到二維空間 X,Y = np.meshgrid(X, Y) #生成一個高 R = np.sqrt(X**2 + Y**2) Z = np.sin(R) #在Axes畫3D圖 #rstride,cstride是行/列跨度 #cmap是畫圖的方式 ax.plot_surface(X,Y,Z, rstride =1, cstride=1, cmap = plt.get_cmap('rainbow')) #畫等高線,zdir表示投射軸 #offset是投射的坐標位置 ax.contourf(X,Y,Z, zdir = 'z', offset = -2, cmap='rainbow') #ax.contourf(X,Y,Z, xdir = 'x', offset = -4, cmap='rainbow') #Z軸的范圍 ax.set_zlim(-2,2) #畫高
Fig 9:
10. 畫子圖
#創建子圖 #把一個fig划分為2*2,即4個圖 #(2,2,1)畫第一張圖 plt.subplot(2,2,1)#plt.subplot(221) plt.plot([0,1],[0,1]) #(2,2,2)畫第二張圖 plt.subplot(2,2,2)#plt.subplot(221) plt.plot([0,1],[0,1]) plt.subplot(2,2,3)#plt.subplot(221) plt.plot([0,1],[0,1])
Fig 10:
11. 圖嵌套
fig = plt.figure() x = [1,2,3,4,5,6,7] y = [1,3,4,2,5,8,6] #其實相當於按比例在fig這張大圖畫各個小圖 #先畫一張比列很大的 #參數是fig的百分比 left, bottom, width, height = 0.1, 0.1, 0.8, 0.9 ax1 = fig.add_axes([left, bottom, width, height]) #給圖畫圖 ax1.plot(x,y,'r') #注意是set_tile ax1.set_title('tile') #再畫小圖 #為小圖加坐標,參數是fig的百分比 left, bottom, width, height = 0.2, 0.7, 0.3, 0.3 ax2 = fig.add_axes([left, bottom, width, height]) #給小圖畫圖 ax2.plot(y,x,'r') ax2.set_title('tile_little')
Fig 11:
12. 共享x軸,不同Y周
x = np.arange(0,10,0.1) y1 = 0.05 *x**2 y2 = -1*y1 #同時創建fig和sub #實際是畫兩張圖 然后重疊,故用subplot fig,ax1 = plt.subplots() #共享x ax2 = ax1.twinx() ax1.plot(x, y1,'g-') ax2.plot(x, y2,'b--') ax1.set_xlabel('x data') ax.set_ylabel('Y1',color = 'g') ax.set_ylabel('Y2',color = 'b') plt.show()
Fig 12: