Can I generate and show a different image during each loop with Matplotlib?

后端 未结 1 482
借酒劲吻你
借酒劲吻你 2020-12-10 08:40

I am new to Matplotlib and Python. I mostly use Matlab. Currently, I am working with a Python code where I want to run a loop. In each loop, I will do some data processing a

相关标签:
1条回答
  • try this:

    import numpy
    from matplotlib import pyplot as plt
    
    if __name__ == '__main__':
        x = [1, 2, 3]
        plt.ion() # turn on interactive mode
        for loop in range(0,3):
            y = numpy.dot(x, loop)
            plt.figure()
            plt.plot(x,y)
            plt.show()
            _ = input("Press [enter] to continue.")
    

    if you want to close the previous plot, before showing the next one:

    import numpy
    from matplotlib import pyplot as plt
    if __name__ == '__main__':
        x = [1, 2, 3]
        plt.ion() # turn on interactive mode, non-blocking `show`
        for loop in range(0,3):
            y = numpy.dot(x, loop)
            plt.figure()   # create a new figure
            plt.plot(x,y)  # plot the figure
            plt.show()     # show the figure, non-blocking
            _ = input("Press [enter] to continue.") # wait for input from the user
            plt.close()    # close the figure to show the next one.
    

    plt.ion() turns on interactive mode making plt.show non-blocking.

    and heres is a duplicate of your matlab code:

    import numpy
    import time
    from matplotlib import pyplot as plt
    
    if __name__ == '__main__':
        x = [1, 2, 3]
        plt.ion()
        for loop in xrange(1, 4):
            y = numpy.dot(loop, x)
            plt.close()
            plt.figure()
            plt.plot(x,y)
            plt.draw()
            time.sleep(2)
    
    0 讨论(0)
提交回复
热议问题