如何設定 Matplotlib 圖形的邊距?


要設定 matplotlib 圖形的邊距,我們可以使用 margins() 方法。

步驟

  • 設定圖形大小並調整子圖之間和子圖四周的邊距。
  • 使用 numpy 建立 ty 資料點。
  • 將子圖新增到當前圖形的索引 1 處。
  • 使用 plot() 方法繪製 t 和 y 資料點。
  • 設定圖形的標題。
  • 將子圖新增到當前圖形的索引 2 處。
  • 使用 plot() 方法繪製 t 和 y 資料點。
  • 設定圖形的標題。
  • 使用 margins(x=0, y=0) 設定圖形的邊距。
  • 要顯示圖形,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

t = np.linspace(-2, 2, 10)
y = np.exp(-t)
plt.subplot(121)
plt.plot(t, y)
plt.title("Without margins")
plt.subplot(122)
plt.plot(t, y)
plt.title("With x=0 and y=0 margins")
plt.margins(x=0, y=0)
plt.show()

輸出

更新於:02-06-2021

5 千次以上瀏覽

開啟您 職業生涯

完成學習以獲得認證

開始
廣告
© . All rights reserved.