Matplotlib subplot2grid()函数详解
生活随笔
收集整理的這篇文章主要介紹了
Matplotlib subplot2grid()函数详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Matplotlib subplot2grid()函數詳解
matplotlib.pyplot 模塊提供了 subplot2grid() ,該函數能夠在畫布的特定位置創建 axes 對象(即繪圖區域)。不僅如此,它還可以使用不同數量的行、列來創建跨度不同的繪圖區域。與 subplot() 和 subplots() 函數不同,subplot2gird() 函數以非等分的形式對畫布進行切分,并按照繪圖區域的大小來展示最終繪圖結果。
函數語法格式如下:
plt.subplot2grid(shape, location, rowspan, colspan)參數含義如下:
shape:把該參數值規定的網格區域作為繪圖區域;
location:在給定的位置繪制圖形,初始位置 (0,0) 表示第1行第1列;
rowsapan/colspan:這兩個參數用來設置讓子區跨越幾行幾列。
下面,在畫布(figure)中添加了行、列跨度均不相同的繪圖子區域,然后在每個繪圖區上,繪制不同的圖形。示例代碼如下:
import matplotlib.pyplot as plt #使用 colspan指定列,使用rowspan指定行 a1 = plt.subplot2grid((3,3),(0,0),colspan = 2) a2 = plt.subplot2grid((3,3),(0,2), rowspan = 3) a3 = plt.subplot2grid((3,3),(1,0),rowspan = 2, colspan = 2) import numpy as np x = np.arange(1,10) a2.plot(x, x*x) a2.set_title('square') a1.plot(x, np.exp(x)) a1.set_title('exp') a3.plot(x, np.log(x)) a3.set_title('log') plt.tight_layout() plt.show()輸出結果如下:
圖1:subplot2grid()輸出結果
總結
以上是生活随笔為你收集整理的Matplotlib subplot2grid()函数详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maven打包报错-There are
- 下一篇: jsp 格式化输出日期,数字