0

I want to create live plots of my results and found this SO question which did most of the job: Matplotlib animation update title using ArtistAnimation

BUT, when I start the script, the first figure shown is every figure at the same time (see screenshot below) What am I doing wrong? Also the axis is plotted twice but shifte (like in the initial figure) for each animation step. I would like to have the first plotted figure as the starting figure and then go to the next one after interval=1000 miliseconds.

Initial Figure

I used the code from the other questions without any additions:

import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np

a = np.random.rand(10,10)

fig, ax=plt.subplots()
container = []

for i in range(a.shape[1]):
    line, = ax.plot(a[:,i])
    title = ax.text(0.5,1.05,"Title {}".format(i), 
                    size=plt.rcParams["axes.titlesize"],
                    ha="center", transform=ax.transAxes, )
    container.append([line, title])

ani = animation.ArtistAnimation(fig, container, interval=200, blit=False)

plt.show()

Using Python=3.7.16 matplotlib==3.5.3

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
JD.
  • 169
  • 2
  • 14

1 Answers1

1

I can't reproduce the issue with a text editor like Sublime. I wonder if you're using Jupyter Notebook! If so, you need to add %matplotlib notebook at the top of the cell.

%matplotlib notebook # <- add this line

...

ani = animation.ArtistAnimation(fig,
                                container,
                                interval=1000, # <- change the interval here
                                blit=False)

Output :

enter image description here

Timeless
  • 22,580
  • 4
  • 12
  • 30
  • No, I am not using jupyter notebook. I also created a new environment where only matplotlib is installed. – JD. Apr 05 '23 at 11:22
  • But you say nothing about where you run your script. – Timeless Apr 05 '23 at 11:22
  • I dont understand the question? What do you mean by "where do you run your script"? The script is executed with `python script.py` using the correct env – JD. Apr 05 '23 at 11:25
  • I can't reproduce the issue neither with such configuration (*e.g a script run from the terminal inside a virtual environement*). So can you try adding this line at the top of your script `import matplotlib;matplotlib.use('TkAgg')` ? Here is a list of the availables backends : https://matplotlib.org/stable/users/explain/backends.html – Timeless Apr 05 '23 at 11:28
  • Nothing changes when using different backends. But thanks for your help! – JD. Apr 05 '23 at 12:52