[Python] How to handle an asymptote or discontinuity

Discussão em 'Python' iniciado por Stack, Setembro 13, 2025.

  1. Stack

    Stack Membro Participativo

    When plotting a graph with a discontinuity, asymptote, singularity, etc., is there an 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 sympy.abc import x

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

    # Set up axis
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['bottom'].set_position('zero')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

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

    # Draw my curve
    myfunction = sympify(1/(x-2))
    mylambdifiedfunction = lambdify(x, myfunction, 'numpy')
    ax.plot(xx, mylambdifiedfunction(xx), zorder=100, linewidth=3, color='red')

    # Set bounds
    ax.set_xbound(-1, 6)
    ax.set_ybound(-4, 4)

    plt.show()


    [​IMG]

    Continue reading...

Compartilhe esta Página