Drawing tangent plot in Python (matplotlib)
Question: Today I decided to write simple program in Python, just to practice before
exam. Firstly, I wanted to draw sin and cos plot, which wasn't so hard. But
then, I decided to challenge myself and draw tangent plot.

    
    
    import pylab as p
    
    x= p.arange(-1.0,1.0,0.1)
    y= (p.sin(2*p.pi*x)) / (p.cos(2*p.pi*x))
    
    p.plot(x,y,'g-',lw=1)
    p.show()
    

It returns... ugh... this:

![enter image description here](http://i.stack.imgur.com/fpANl.png)

As you can see, it looks more like ECK plot than tangent plot. Does anyone
knows what's wrong?


Answer: If you increase the number of points in `x`,

    
    
    import pylab as p
    import numpy as np
    x = p.linspace(-1.0, 1.0, 1000)
    y = (p.sin(2 * p.pi * x)) / (p.cos(2 * p.pi * x))
    p.plot(x, y, 'g-', lw=1)
    p.show()
    

you get something like this: ![enter image description
here](http://i.stack.imgur.com/vklgJ.png)

Notice how large the `y-range` is getting. Matplotlib is not able to show you
much of the small values in the tangent curve because the range is so large.

The plot can be improved by ignoring the extremely large values near the
asymptotes. Using [Paul's
workaround](http://stackoverflow.com/a/2542065/190597) to handle asymptotes,

    
    
    import pylab as p
    import numpy as np
    x = p.linspace(-1.0, 1.0, 1000)
    y = (p.sin(2 * p.pi * x)) / (p.cos(2 * p.pi * x))
    
    tol = 10
    y[y > tol] = np.nan
    y[y < -tol] = np.nan
    
    p.plot(x, y, 'g-', lw=1)
    p.show()
    

you get

![enter image description here](http://i.stack.imgur.com/XzLmQ.png)

