2012-12-13 177 views
2

我試圖用pyplot和matplotlib繪製雙曲線。這是我的代碼:試圖在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() 

它產生如下圖:

hyperbola

如何修改我的圖形刪除了這一行跑下來的垂直漸近線?

+2

也許這[StackOverflow問題](http://stackoverflow.com/questions/2540294/how-to-handle-an-asymptote-discontinuity-with-matplotlib)是否有幫助? – nvlass

+0

啊,我完全錯過了這個問題。我要把我的標記作爲一個副本。 – Michael0x2a

回答

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 

希望這有助於。