有排除函数是matplotlib吗?

Is there an exclusion function is matplotlib?

我想在没有任何垂直渐近线的情况下在 matplotlib 中绘制以下函数:

f(x) = 1/(x - 1)

matplotlib中有没有排除函数可以mask/hide类似的渐近线 Exlusions 在 Mathematica 中

答案here似乎没有帮助。

我假设您只尝试了第一个答案。当您阅读一个有多个答案的堆栈溢出问题并且第一个不适合您时,您应该尝试下一个。

这些解决方案背后的想法是屏蔽掉您不感兴趣的值。在这里你看到:

  • 带有丑陋渐近线的原始情节。
  • 第一种方法:我们准确知道渐近线的位置,x=1。因此,我们可以将所有 y 值隐藏在 x=1.
  • 附近的一个小区域中
  • 第二种方法:在渐近线附近值趋于无穷大。我们可以隐藏所有大于(或小于)某个阈值的值。

肯定有其他方法可以解决这个问题,但前两种方法对于简单的绘图来说是快速有效的。

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.5, 1.5, 1000)
y = 1 / (x - 1)

f, ax = plt.subplots(2, 2)
ax[0, 0].plot(x, y)
ax[0, 0].set_title("Original")

y2 = y.copy()
# select all values of y2 where x > 0.99
# and x < 1.01. You can play with these numbers
# to get the desired output.
y2[(x > 0.99) & (x < 1.01)] = np.nan
ax[0, 1].plot(x, y2)
ax[0, 1].set_title("First method")

y3 = y.copy()
# hide all y3 values whose absolute value is
# greater than 250. Again, you can change this
# number to get the desired output.
y3[y3 > 250] = np.nan
y3[y3 < -250] = np.nan
ax[1, 0].plot(x, y3)
ax[1, 0].set_title("Second method")

plt.tight_layout()
plt.show()