Python 尝试在matplotlib中绘制双曲线会沿垂直渐近线生成直线吗?
我试图用pyplot和matplotlib绘制一条双曲线。这是我的代码:Python 尝试在matplotlib中绘制双曲线会沿垂直渐近线生成直线吗?,python,matplotlib,graphing,Python,Matplotlib,Graphing,我试图用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.s
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()
它生成以下图形:
如何修改图形以删除沿垂直渐近线延伸的直线?打印前,添加以下直线:
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
希望这能有所帮助。也许这很有帮助?啊,我完全错过了那个问题。我要把我的标记为复制品。