如何在 matplotlib 中沿著 X 軸移動圖形?


要在 matplotlib 中沿著 X 軸移動圖形,我們可以採取以下步驟 -

  • 設定圖形尺寸並調整子圖之間和周圍的內邊距。
  • 使用 Numpy 建立 x 和 y 資料點。
  • 為原始曲線繪製 x 和 y 資料點。
  • 繪製平移後的圖形,範圍為 (1, 1+len(y)),帶有 y 資料點。
  • 在圖形上放置圖例。
  • 要顯示圖形,請使用 **show()** 方法。

示例

import numpy as np
import matplotlib.pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# x and y data points
x = np.linspace(-5, 5, 100)
y = np.sin(x)

# Original graph and shifted graph
plt.plot(x, y, label='Original Graph')
plt.plot(range(1, 1+len(y)), y, label='Shifted Graph')

# Place a legend
plt.legend(loc='upper right')

plt.show()

輸出

它將生成以下輸出

更新於: 2021 年 9 月 22 日

9K+ 瀏覽

開始你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.