how to handle an asymptote/discontinuity with Matplotlib

Posted on

Question :

how to handle an asymptote/discontinuity with Matplotlib

When plotting a graph with a discontinuity/asymptote/singularity/whatever, is there any automatic way to prevent Matplotlib from ‘joining the dots’ across the ‘break’? (please see code/image below).
I read that Sage has a [detect_poles] facility that looked good, but I really want it to work with Matplotlib.

import matplotlib.pyplot as plt 
import numpy as np
from sympy import sympify, lambdify
from import x

fig = plt.figure(1) 
ax = fig.add_subplot(111) 

# set up axis 

# setup x and y ranges and precision
xx = np.arange(-0.5,5.5,0.01) 

# draw my curve 
ax.plot(xx, mylambdifiedfunction(xx),zorder=100,linewidth=3,color='red') 

#set bounds 


Asked By: Geddes


Answer #1:

This may not be the elegant solution you are looking for, but if just want results for most cases, you can “clip” large and small values of your plotted data to +? and -? respectively. Matplotlib does not plot these. Of course you have to be careful not to make your resolution too low or your clipping threshold too high.

utol = 100.
ltol = -100.
yy = 1/(xx-2)
yy[yy>utol] = np.inf
yy[yy<ltol] = -np.inf

ax.plot(xx, yy, zorder=100, linewidth=3, color='red') 
Answered By: Paul

Answer #2:

By using masked arrays you can avoid plotting selected regions of a curve.

To remove the singularity at x=2:

import as M    # for older versions, prior to .98
#import as M                # for newer versions of matplotlib
from pylab import *


xx = np.arange(-0.5,5.5,0.01) 
vals = 1/(xx-2)        
vals = M.array(vals)
mvals = M.masked_where(xx==2, vals)

plot(xx, mvals, linewidth=3, color='red') 

This simple curve might be a bit more clear on which points are excluded:

xx = np.arange(0,6,.2) 
vals = M.array(xx)
mvals = M.masked_where(vals%2==0, vals)
plot(xx, mvals, color='b', linewidth=3)
plot(xx, vals, 'rx')

enter image description here

Answered By: tom10

Answer #3:

No, I think there is no built-in way to tell matplotlib to ignore these
points. After all, it just connects points and knows nothing about functions
or what happens in between the points.

However, you can use sympy to find the poles, and then patch the continuous pieces of your function together. Here some admittedly ugly code that does exactly that:

from pylab import *
from sympy import solve
from import x
from sympy.functions.elementary.complexes import im

xmin = -0.5
xmax = 5.5
xstep = 0.01

# solve for 1/f(x)=0 -- we will have poles there
discontinuities = sort(solve(1/(1/(x-2)),x))

# pieces from xmin to last discontinuity
last_b = xmin
for b in discontinuities:
    # check that this discontinuity is inside our range, also make sure it's real
    if b<last_b or b>xmax or im(b):
    xi = np.arange(last_b, b, xstep)
    plot(xi, 1./(xi-2),'r-')
    last_b = b

# from last discontinuity to xmax
xi = np.arange(last_b, xmax, xstep)
plot(xi, 1./(xi-2),'r-')

xlim(xmin, xmax)


Answered By: Benjamin Bannier

Leave a Reply

Your email address will not be published. Required fields are marked *