[Python] Why do I get a division by zero error when plotting inverse function on secondary...

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I get a division by zero error when I run the following python script.

    import matplotlib.pyplot as plt

    fig, ax = plt.subplots(figsize=(10, 10))

    freq = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 1.8, 2.0]
    amp = [0.0, 0.0, 0.0, 0.0, 0.0, 0.012, 0.031, 0.074, 0.082, 0.084, 0.080, 0.078, 0.072, 0.059, 0.039, 0.019, 0.010]
    ax.semilogx(freq, amp, marker='s', color='purple')
    plt.xlim(0.1, 10)
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Crest Loss (ft)')
    ax.set(title='Fundamental Frequency')
    ax.grid()
    ax.grid(which="minor", color="0.9")

    def forward(x):
    return 1 / x

    def inverse(x):
    return 1 / x

    secax = ax.secondary_xaxis('top', functions=(forward, inverse))
    secax.set_xlabel('Period (s)')

    plt.show()


    The plot seems to output correctly, but I don't know why I get a division by zero error when all the data plotted on the x axis is greater than zero.

    Continue reading...

Compartilhe esta Página