Home > Software engineering >  Simple Python animation using pause command not working
Simple Python animation using pause command not working

Time:01-28

I wanted to create a simple animation using plt.pause() command in python. So I tried the first example from this page. If you go there, it shows you a nice animation of a graph growing smoothly. I copy-paste it below

from matplotlib import pyplot as plt 
x = []
y = []
for i in range(100):
    x.append(i)
    y.append(i)

# Mention x and y limits to define their range
    plt.xlim(0, 100)
    plt.ylim(0, 100)
  
# Ploting graph
    plt.plot(x, y, color = 'green')
    plt.pause(0.01)
plt.show()

However, it is giving me 100 different plots with an incremental increase until it completes the range. I think the problem is with figures appearing without waiting for the plt.show() command? Help me. I am using Jupiter 6.0.3 and sypder 4.1.3

CodePudding user response:

Without the .show() command the plot will disappear at the completion of the code. however the animation should still be visible as it is created by the .plot() command.

I am not able to reproduce your problem using Python 3.9.7.

You can always try running the script outside of the IDE using python pathtoscript and see if that resolves the issue.

CodePudding user response:

To build on @Regretful's answer, if you're running in a Jupyter notebook, use %matplotlib qt or some other non-inline backend to get the usual pop-up window, rather than static images.

No idea for Spyder, though!

  •  Tags:  
  • Related