【问题标题】:Attempting to draw hyperbola in matplotlib produces line along vertical asymptote? [duplicate]尝试在 matplotlib 中绘制双曲线会沿垂直渐近线产生线吗? [复制]
【发布时间】:2012-12-13 17:26:52
【问题描述】:

我正在尝试使用 pyplot 和 matplotlib 绘制双曲线。这是我的代码:

from __future__ import division

import numpy
import matplotlib.pyplot as pyplot

x = numpy.arange(0, 1000, 0.01)
y = [10 / (500.53 - i) for i in x]
pyplot.plot(x, y, 'b')
pyplot.axis([0, 1000, -10, 10])

pyplot.show()

它会生成以下图表:

如何修改我的图表以删除沿垂直渐近线延伸的那条线?

【问题讨论】:

  • 也许这个StackOverflow Question 有帮助?
  • 啊,我完全错过了这个问题。我要把我的标记为重复。

标签: python matplotlib graphing


【解决方案1】:

在绘图之前,添加这些行:

 threshold = 1000 # or a similarly appropriate threshold
 y = numpy.ma.masked_less(y, -1*threshold) 
 y = numpy.ma.masked_greater(y, threshold).

然后做

pyplot.plot(x, y, 'b')
pyplot.axis([0, 1000, -10, 10])
pyplot.show()

像往常一样。

还请注意,由于您使用的是 numpy 数组,因此您不需要列表推导来计算 y

In [12]: %timeit y = [10 / (500.53 - i) for i in x]
1 loops, best of 3: 202 ms per loop

In [13]: %timeit y = 10 / (500.53 - x)
1000 loops, best of 3: 1.23 ms per loop

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多