如何使用 Matplotlib 處理漸近線/不連續性?


若要使用 matplotlib 處理漸近線/不連續性,可以按以下步驟執行 −

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

  • 關閉軸圖。

  • 繪製具有 x 和 y 資料點的線。

  • 在 x=0 軸上新增一條水平線。

  • 在 y=0 軸上新增一條豎直線。

  • 放置 y=1/x 曲線的圖例。

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

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-1, 1, 100)
y = 1 / x
plt.axis('off')
plt.plot(x, y, label='y=1/x')
plt.axhline(y=0, c='red')
plt.axvline(x=0, c='red')
plt.legend(loc='upper left')
plt.show()

輸出

更新於:2021-05-11

1K+ 檢視次數

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.