且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 y=1/x 绘制为单个图形

更新时间:2023-11-21 23:03:28

实际上你想包含 x = 0 因为这导致 y = nan,形成一个缺口在情节中.

Actually you want to include x = 0 because this results in y = nan, forming a gap in the plot.

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return 1/x
fx_name = r'$f(x)=\frac{1}{x}$'

# using 101 steps results in in array including the value 0
x=np.linspace(-10,10,101)
# f(0) = nan -> a nan value creates a gap
y=f(x)
plt.plot(x, y, label=fx_name)
plt.legend(loc='upper left')
plt.show()