【ref】matplotlib中的plt.figure()、plt.subplot()、plt.subplots()、add_subplots以及add_axes的使用

matplotlib中的plt.figure()、plt.subplot()、plt.subplots()、add_subplots以及add_axes的使用

轉載C小C 最後發佈於2018-11-12 16:51:33 閱讀數 7960  收藏

展開

【時間】2018.11.12

【題目】matplotlib中的plt.figure()、plt.subplot()、plt.subplots()、add_subplots以及add_axes的使用

概述

本文是博文https://blog.csdn.net/m0_37362454/article/details/81511427的簡要概括,具體內容請看原博文。主要講述了matplotlib中的plt.figure()、plt.subplot()、plt。subplots()\add_subplots以及add_axes的語法與使用方法。

一.plt.figure語()---在plt中繪製一張圖片

1.1figure語法說明

figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

  • num:圖像編號或名稱,數字爲編號 ,字符串爲名稱
  • figsize:指定figure的寬和高,單位爲英寸;
  • dpi參數指定繪圖對象的分辨率,即每英寸多少個像素,缺省值爲80      1英寸等於2.5cm,A4紙是 21*30cm的紙張 
  • facecolor:背景顏色
  • edgecolor:邊框顏色
  • frameon:是否顯示邊框

1.2例子

【代碼】:


 
  1. import matplotlib.pyplot as plt

  2. fig=plt.figure(figsize=(4,3),facecolor='blue')

  3. plt.show()

【運行結果】:

2.subplot--創建單個子圖

2.1.subplot語法

subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)

2.2例子

【代碼】


 
  1. import numpy as np

  2. import matplotlib.pyplot as plt

  3. x = np.arange(0, 100)

  4.  
  5. plt.subplot(221)

  6. plt.plot(x, x)

  7. #作圖2

  8. plt.subplot(222)

  9. plt.plot(x, -x)

  10. #作圖3

  11. plt.subplot(223)

  12. plt.plot(x, x ** 2)

  13. plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)

  14. #作圖4

  15. plt.subplot(224)

  16. plt.plot(x, np.log(x))

  17. plt.show()

【運行結果】

三.subplots--創建多個子圖

3.1subplots語法

subplots參數與subplots相似。兩者都可以規劃figure劃分爲n個子圖,但每條subplot命令只會創建一個子圖,而一條subplots就可以將所有子圖創建好。

3.例子

【代碼】


 
  1. import numpy as np

  2. import matplotlib.pyplot as plt

  3.  
  4. x = np.arange(0, 100)

  5. #劃分子圖

  6. fig,axes=plt.subplots(2,2)

  7. ax1=axes[0,0]

  8. ax2=axes[0,1]

  9. ax3=axes[1,0]

  10. ax4=axes[1,1]

  11.  
  12. #作圖1

  13. ax1.plot(x, x)

  14. #作圖2

  15. ax2.plot(x, -x)

  16. #作圖3

  17. ax3.plot(x, x ** 2)

  18. ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)

  19. #作圖4

  20. ax4.plot(x, np.log(x))

  21. plt.show()

【運行結果】

四、add_subplot方法----給figure新增子圖

4.1語法

add_subplot()的作用與subplot一樣,用於新增子圖。具體如下:

#新建figure對象
fig=plt.figure()
#新建子圖1,(2,2,1)表示創建2x2子圖中的第一個
ax1=fig.add_subplot(2,2,1)     

4.2 例子

【代碼】


 
  1. import numpy as np

  2. import matplotlib.pyplot as plt

  3. x = np.arange(0, 100)

  4. #新建figure對象

  5. fig=plt.figure()

  6. #新建子圖1

  7. ax1=fig.add_subplot(2,2,1)

  8. ax1.plot(x, x)

  9. #新建子圖2

  10. ax3=fig.add_subplot(2,2,2)

  11. ax3.plot(x, x ** 2)

  12. ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)

  13. #新建子圖3

  14. ax4=fig.add_subplot(2,2,3)

  15. ax4.plot(x, np.log(x))

  16. plt.show()

 

【運行結果】

五、add_axes方法----新增子區域

5.1語法

add_axes爲新增子區域,該區域可以坐落在figure內任意位置,且該區域可任意設置大小。具體如下:

 fig.add_axes([left, bottom, width, height])

其中left, bottom表示左邊以及下邊的起始位置,width, heigh表示寬高佔原fig的比例。比如:

left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 表示子區域從figure 10%的位置開始繪製, 寬高是figure的80%

5.2例子

【代碼】:


 
  1.  
  2. import matplotlib.pyplot as plt

  3.  
  4. #新建figure

  5. fig = plt.figure()

  6. # 定義數據

  7. x = [1, 2, 3, 4, 5, 6, 7]

  8. y = [1, 3, 4, 2, 5, 8, 6]

  9. #新建區域ax1

  10. #figure的百分比,從figure 10%的位置開始繪製, 寬高是figure的80%

  11. left, bottom, width, height = 0.1, 0.1, 0.8, 0.8

  12. # 獲得繪製的句柄

  13. ax1 = fig.add_axes([left, bottom, width, height])

  14. ax1.plot(x, y, 'r')

  15. ax1.set_title('area1')

  16.  
  17. #新增區域ax2,嵌套在ax1內

  18. left, bottom, width, height = 0.2, 0.6, 0.25, 0.25

  19. # 獲得繪製的句柄

  20. ax2 = fig.add_axes([left, bottom, width, height])

  21. ax2.plot(x,y, 'b')

  22. ax2.set_title('area2')

  23. plt.show()

【運行結果】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章