0

I try to draw lines with Matplotlib Animation from numpy array of (3000, 2) shape. My code is a compilation from matplotlib examples and this answer :

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

value.shape # pairs of x,y values
fig = plt.figure()
ax = fig.gca()

def init():
    ax.clear()
    ax.set_xlim(0, 1600)
    ax.set_ylim(0, 900)

def update(i): 
    if not i:
        init()
        
    ax.plot([values[i,0], values[i+1, 0]], [values[i,1], values[i+1, 1]], "blue") 

ani = anim.FuncAnimation(fig, update, init_func=init, frames = 100, interval = 300, repeat=True)
plt.show()
ani.save("animation.mp4")

The code executes without errors, but for some reasons it doesn't draw lines. How can i fix it?

gipulo
  • 1
  • Maybe call `plt.show()` after `ani.save(...)`? `plt.show()` clears everything when done. – JohanC Mar 22 '21 at 12:05
  • @JohanC i swapped the lines, nothing changed – gipulo Mar 22 '21 at 12:26
  • I would like to try to run your code, but I do not have the data. Can you post the data from the description as I cannot image it? – r-beginners Mar 22 '21 at 13:06
  • @r-beginners I changed the line `value.shape` to `values = np.random.rand(200, 2) * 900` and the code ran as expected and produced a working animation. – tmdavison Mar 22 '21 at 13:12
  • 1
    @gipulo make sure your data is in the range (0-1600) for the x values and (0-900) for the y values. Also, you have a line that says `value.shape` at the beginning, but then index an array called `values`. Make sure your naming is consistent throughout the script. If it still doesn't work, create a [MCVE] and come back here with more details of how it doesn't work – tmdavison Mar 22 '21 at 13:14

0 Answers0