如何在 Python 中用 Matplotlib 繪製階躍函式?


若要在 Python 中用 matplotlib 繪製階躍函式,我們可以按照以下步驟操作——

  • 為 x 和 y 建立資料點。

  • 使用 step() 方法建立階躍圖。

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

示例

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
plt.step(x, y, 'r*')
plt.show()

輸出

更新於:2021 年 5 月 8 日

1K+ 閱讀

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.