在 Matplotlib 中,set_xlim 和 set_xbound 之間有什麼區別?


set_xlim − 設定 X 軸的檢視限制。

set_xbound − 設定 X 軸的上下數值界限。

要設定 xlim 和 xbound,我們可以採取以下步驟 −

  • 使用 subplots(2),我們可以建立一個圖形和一組子圖。此處,我們將建立 2 個子圖。

  • 使用 numpy 建立 x 和 y 資料點。

  • 使用 plot() 方法,使用第 1 軸繪製 x 和 y 資料點。

  • 使用 set_xlim() 方法設定 x 軸限制。

  • 使用繪製 x 和 y 資料點,使用 plot() 方法的第 2 軸。

  • 使用 set_xbound() 方法設定xbound 

  • 要顯示圖形,請使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, axes = plt.subplots(2)

x = np.linspace(-5, 5, 100)
y = np.sin(x)

axes[0].plot(x, y, c='g')
axes[0].set_xlim(-3, 3)

axes[1].plot(x, y, c='r')
axes[1].set_xbound(-3, 3)

plt.show()

輸出

更新於: 11-May-2021

383 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.