[Python] Matplotlib 基本教學

程式語言:Python
Package:matplotlib
官網
官方圖廊

功能:畫圖

基本概念

  • Figure
    • 最上層的物件
    • 包含 Axes 物件,甚至其他的元件
  • Axes
    • 常用的畫圖物件,包含座標軸,像是直方圖、折線圖、圓餅圖等
  • Axis
    • 座標軸物件
  • 若使用 plt.method,其實就是在呼叫當前的 axes 進行畫圖
    • matplotlib.pyplot.gcf()
      • 得到當前的 figure
    • matplotlib.pyplot.gca()
      • 得到當前的 axes
    • matplotlib.pyplot.sca(ax)
      • 設定當前的 axes,並且會自動更正 figure
  • 參數對應圖

線圖

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # 建立 x,從 -pi 到 pi,共 100 點,且需包含終點 pi
  5. x = np.linspace(-np.pi, np.pi, num=100, endpoint=True)
  6. c,s,t = np.cos(x), np.sin(x), np.tan(x)
  7.  
  8. # 第一次呼叫 plt.plot 會自動建立合適的 figure
  9. # 之後若有使用到 plt.method 皆以此 figure 為主
  10. # 可以使用 latex 語法,需用 $ ... $ 包住
  11. plt.plot(x, c, label=r'cos$\theta$')
  12.  
  13. # 之後呼叫 plt.plot 會使用先前建立的 figure
  14. # 畫出紅色向下三角形
  15. plt.plot(x, s, 'rv', label=r'sin$\theta$')
  16. # 畫出綠虛線,並在各點位置標上綠線青圓點
  17. plt.plot(x, t, color='g', linestyle='dashed', marker='o', markerfacecolor='c', markersize=5)
  18.  
  19. # 以下為特調需求,不見得需要
  20. # ---------------------------------------------
  21. # 用来正常顯示中文標簽
  22. plt.rcParams['font.sans-serif']=['SimHei']
  23.  
  24. # 用来正常顯示負號
  25. plt.rcParams['axes.unicode_minus']=False
  26.  
  27. # 設定 x軸 label
  28. plt.xlabel('X軸')
  29. # 設定 x軸對應的刻度,並替換為文字
  30. plt.xticks( [-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])
  31. # 設定 x軸左右範圍
  32. xmin ,xmax = x.min(), x.max()
  33. dx = (xmax - xmin) * 0.2
  34. plt.xlim(xmin - dx, xmax + dx)
  35.  
  36. # 設定 y軸 label
  37. plt.ylabel('Y軸')
  38. # 設定 x軸對應的刻度
  39. plt.yticks([-1, 0, +1])
  40. # 設定 y軸上下範圍
  41. ymin ,ymax = c.min(), c.max()
  42. dy = (ymax - ymin) * 0.2
  43. plt.ylim(ymin - dy, ymax + dy)
  44.  
  45. plt.title('標題')
  46.  
  47. # 放置文字
  48. plt.text(0, 1.2, '我在這', color='red')
  49.  
  50. # 需有 label 才會顯示,所以 tanθ 並無顯示
  51. plt.legend()
  52.  
  53. # 建立中心坐標軸
  54. # 回傳目前的坐標軸 get current axes
  55. ax = plt.gca()
  56. # 將上跟右的線,設成隱藏
  57. ax.spines['right'].set_color('none')
  58. ax.spines['top'].set_color('none')
  59.  
  60. # 設定 x軸刻度顯示在下方
  61. ax.xaxis.set_ticks_position('bottom')
  62. # 設定下方的線,移動至 data 為 -1 的地方
  63. ax.spines['bottom'].set_position(('data',-1))
  64. # 設定 y軸刻度顯示在左方
  65. ax.yaxis.set_ticks_position('left')
  66. # 設定左方的線,移動至坐標軸中心
  67. ax.spines['left'].set_position(('axes', 0.5))
  68. # ---------------------------------------------
  69.  
  70. # 顯示圖
  71. plt.show()

分隔圖

兩種方法
subplot
較簡單,但自定性不高
  1. import matplotlib.pyplot as plt
  2.  
  3. # 建立第一張圖,若直接 plt.plot 隱含自動建立 figure 並建立 subplot(111)
  4. plt.figure(1)
  5. # 第一張圖的第一張子圖,231 表示大小為 row:2 col:3 的第一張,index 從 1 開始
  6. plt.subplot(231)
  7. plt.plot([1,2,3])
  8.  
  9. # 第一張圖的第五張子圖,等同235,index 從 1 開始
  10. # plt.subplot(nrows, ncols, plot_number)
  11. # 但 plot_number 不是參數名,所以無法寫成 plt.subplot(nrows=2, ncols=3, plot_number=5)
  12. plt.subplot(2, 3, 5)
  13. plt.plot([4,5,6])
  14. # 建立第二張圖,若無建立,以下行為會覆蓋之前的圖
  15. plt.figure(2)
  16. # 默認建立子圖 subplot(111)
  17. plt.plot([4,5,6])
  18. # 切換到 figure 1 ; 子圖 subplot(235) 仍是當前圖
  19. plt.figure(1)
  20. # 加入 subplot 235 的標题
  21. plt.title('fifth', color='r')
  22.  
  23. # 顯示圖
  24. plt.show()

subplot 搭配 gridspec
  1. import matplotlib.pyplot as plt
  2. import matplotlib.gridspec as gridspec
  3.  
  4. # 建立 3x3 的 GridSpec
  5. gs = gridspec.GridSpec(2, 3)
  6.  
  7. # 建立第一張圖,若直接 plt.plot 隱含自動建立 figure 並建立 subplot(111)
  8. plt.figure(1)
  9. # 第一張圖的 (0,-1) 子圖,index 從 0 開始
  10. plt.subplot(gs[0,-1])
  11. plt.plot([1,2,3])
  12. # 第一張圖的第二欄,index 從 0 開始,也可用 [1,0:3] 表示
  13. plt.subplot(gs[1,:])
  14. plt.plot([4,5,6])
  15. # 建立第二張圖,若無建立,以下行為會覆蓋之前的圖
  16. plt.figure(2)
  17. # 默認建立子圖 subplot(111)
  18. plt.plot([4,5,6])
  19. # 切換到 figure 1 ; 子圖 (1,:) 仍是當前圖
  20. plt.figure(1)
  21. # 切換到 (0,-1) 成為 figure1 的當前圖
  22. plt.subplot(gs[0,-1])
  23. # 加入 subplot (0,-1) 的標题
  24. plt.title('(0,-1)', color='r')
  25.  
  26. # 顯示圖
  27. plt.show()

直方圖

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4.  
  5. np.random.seed(0)
  6.  
  7. # 平均 200,標準差為 25 的分佈
  8. mu = 200
  9. sigma = 25
  10. x = np.random.normal(mu, sigma, size=100)
  11.  
  12. # 擁有兩張子圖,大小為 15x4 inch,dpi 分辨率 為 100 px/inch,故圖大小為 1500x400 px
  13. fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(15, 4), dpi=100)
  14.  
  15. # 分成 5 組,背景為透明度為 0.75 的綠色,並且縱軸不做正規化處理為數量,直條的間距填滿
  16. ax0.hist(x, 5, normed=0, histtype='stepfilled', facecolor='g', alpha=0.75)
  17. ax0.set_title('stepfilled\n' + r'$\mu = 200, \sigma=25$')
  18.  
  19. # 自定分組,縱軸執行正規化處理表示為機率,直條的寬度大小為 80%
  20. bins = [100, 150, 180, 195, 205, 220, 250, 300]
  21. ax1.hist(x, bins, normed=1, histtype='bar', rwidth=0.8)
  22. ax1.set_title('unequal bins\n' + r'$\mu = 200, \sigma=25$')
  23.  
  24. # 緊密排列,並填滿原圖大小
  25. fig.tight_layout()
  26.  
  27. # 顯示圖
  28. plt.show()

散點圖

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4.  
  5. N = 150
  6. # 產生 150 個 0~2 之間的隨機半徑
  7. r = 2 * np.random.rand(N)
  8. # 產生 150 個 0~2pi 之間的隨機弧度
  9. theta = 2 * np.pi * np.random.rand(N)
  10. # 區域大小與半徑成正比
  11. area = 50 * r**2
  12. # 顏色由弧度決定
  13. colors = theta
  14.  
  15. ax = plt.subplot(211)
  16. c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
  17.  
  18. # 畫出極座標圖,此時的 x 為弧度,y 為半徑
  19. ax = plt.subplot(212, projection='polar')
  20. c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
  21.  
  22. plt.show()

圓餅圖

  1. import matplotlib.pyplot as plt
  2.  
  3. labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
  4. sizes = [15, 30, 45, 10]
  5. # 分離係數,所以只有 'Hogs' 會分離
  6. explode = (0, 0.1, 0, 0)
  7.  
  8. fig1, ax1 = plt.subplots()
  9. # 從 90° 開始,逆時針排列,並加上陰影,並加上每塊的比例,格式為 '%1.2f%%'
  10. ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.2f%%', shadow=True, startangle=90)
  11. # 調整比例,確認顯示為圓形
  12. ax1.axis('equal')
  13.  
  14. plt.show()

參考

Matplotlib 教程
Python--matplotlib绘图可视化知识点整理

留言