3

I'm attempting to draw a hyperbola using pyplot and matplotlib. This is my code:

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()

It produces the following graph:

hyperbola

How can I modify my graph to remove that line running down the vertical asymptote?

Community
  • 1
  • 1
Michael0x2a
  • 41,137
  • 26
  • 119
  • 175
  • 2
    Maybe this [StackOverflow Question](http://stackoverflow.com/questions/2540294/how-to-handle-an-asymptote-discontinuity-with-matplotlib) is helpfull? – nvlass Dec 13 '12 at 17:31
  • Ah, I totally missed that question. I'm going to flag mine as a duplicate. – Michael0x2a Dec 13 '12 at 17:34

1 Answers1

1

Before plotting, add these these lines:

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

And then do

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

as you normally would.

Also note that since you're using numpy arrays, you don't need the list comprehension to compute 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

Hope this helps.

Paul H
  • 52,530
  • 16
  • 137
  • 125