python下matplotlib的subplot的多圖顯示位置的問題


1.說明

1.1 多圖:

221,222

212

------------附最后講解,這下更清楚了吧,取個名字:顛倒一下---

1.2 多圖

211

223,224

------------附最后講解,這下更清楚了吧-----

2.圖

3.代碼

import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 3.0, 0.01)

#表1
#ax1 = plt.subplot(221)
ax1 = plt.subplot(2,2,1) #221=2,2,1
ax1.margins(2, 2)           # Values >0.0 zoom out
ax1.plot(t1, f(t1), 'r')
ax1.set_title('pic-1')
#表2
ax2 = plt.subplot(222) #222=2,2,2
ax2.margins(x=0, y=-0.25)   # Values in (-0.5, 0.0) zooms in to center
ax2.plot(t1, f(t1), 'g')
ax2.set_title('pic-2')

#表3
#ax3 = plt.subplot(212) #212=2,1,2=223+224=第2行,第1列,占2個位置?
ax3 = plt.subplot(2,1,2)
ax3.margins(0.05)           # Default margin is 0.05, value 0 means fit
ax3.plot(t1, f(t1), 'k')
ax3.set_title('pic-3')

plt.show()#展現圖片

 

4.說明

subplot的參數221意思就是,把整個大的畫板分成2*2,這個子圖在第1個位置,占一個位置

因為分成了2*2,那么還可以有222,223,224.
如下布局
---------
221 222
223 224
---------
plt.subplot(2,2,1)表示將整個圖像窗口分為2行2列, 當前位置為1.

教學地址
https://morvanzhou.github.io/tutorials/data-manipulation/plt/4-1-subpot1/

未說明:212=223+224=第2行,第1列,占2個位置
 
5.顛倒一下,更清楚
5.1 多圖顯示
--------------------------
211(221,222)
223 , 224(212)
------------------------
看懂了么?我搞懂了。
5.2 代碼:修改一下
import numpy as np
import matplotlib.pyplot as plt

def f(t):
    return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 3.0, 0.01)

#表1
ax1 = plt.subplot(223) 
ax1.margins(2, 2)        
ax1.plot(t1, f(t1), 'r')
ax1.set_title('pic-1')
#表2
ax2 = plt.subplot(224) 
ax2.margins(x=0, y=-0.25)  
ax2.plot(t1, f(t1), 'g')
ax2.set_title('pic-2')

#表3
ax3 = plt.subplot(211)
ax3.margins(0.05)          
ax3.plot(t1, f(t1), 'k')
ax3.set_title('pic-3')

plt.show()#展現圖片

5.3 圖

 
 
 
 
 
 
 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM