[Python] How do i update a plot inside a loop python

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm measuring the time it takes to solve a linear system. Im comparing the time it takes between the solve and inv linalg functions. This is what i have so far.

    import numpy as np
    from datetime import datetime
    import matplotlib.pyplot as plt
    for n in (100,200,500,1000,2000,5000,10000):

    A = np.random.randn(n,n)
    B = np.ones((n,1))

    # solving using INV FNC

    start_time = datetime.now()
    Xi = np.linalg.inv(A)*B
    end_time = datetime.now()

    # solving using SOLVE FNC

    start_time2 = datetime.now()
    Xs = np.linalg.solve(A,B)
    end_time2 = datetime.now()


    print('Duration of INV FNC for n: {}'.format(end_time - start_time))
    print('Duration of SOLVE FNC for n: {}'.format(end_time2 - start_time2))


    Pretty much I'm not sure how to make a plot that updates inside the loop showing the comparison of the time it takes to solve the different functions. The plot should have n on the x axis (as in size of the matrices), and then the time takes so solve on the y. Im stuck :(

    Continue reading...

Compartilhe esta Página