2

I have this example code and it works. I'm really really new to python, and I come from Matlab and Octave. I need to manipulate variables inside a FOR loop (in different and more complex ways than in the given example), then I need the animation output in a gif file. With this code, I visualize the animation correctly with the fps I need, but I'm not able to find a simple way to save the animation I see when plt.show() into a gif file or into multiple png files. How could I do it ?

# basic animated mod 39 wheel in python


import matplotlib.pyplot as plt
import numpy as np 
plt.close()
plt.rcParams.update({
    "lines.color": "white",
    "patch.edgecolor": "white",
    "text.color": "lightgray",
    "axes.facecolor": "black",
    "axes.edgecolor": "lightgray",
    "axes.labelcolor": "white",
    "xtick.color": "white",
    "ytick.color": "white",
    "grid.color": "lightgray",
    "figure.facecolor": "black",
    "figure.edgecolor": "black",
    "savefig.facecolor": "black",
    "savefig.edgecolor": "black"})
plt.xlabel('real axis')
plt.ylabel('imaginary axis')
plt.title('events constellation')
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.gca().set_aspect('equal', adjustable='box')
plt.draw()
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

for n in range(1,40):

    cnums = 3 * np.exp(1j * 2 * np.pi * (1/39) * n)
    x = cnums.real 
    y = cnums.imag 
    plt.scatter(x, y , label="event", marker="o", color="red", s=200)
    plt.pause(1)

plt.show()


  • 1
    This may help you: https://eli.thegreenplace.net/2016/drawing-animated-gifs-with-matplotlib/ – Niloct Jan 27 '20 at 03:10
  • Hi, thanks for the reply. I already checked that link before I asked, but I couldnt get how to add a simple command after or before I do plt.show() in my code to save the same animation in a gif, avi or multiple png files. Any of those options would help – symbols_pangaea Jan 27 '20 at 03:15

2 Answers2

1

If you want to export images, there are plenty of answers to your problems on the board. for example: https://stackoverflow.com/a/9890599/6102332

If you want to export to gif or mp4 etc. Similarly, there are many great replies on the forum. for example: https://stackoverflow.com/a/25143651/6102332


In particular about your code: If you want to save multiple images, you can put this line at the end of the for loop:

for n in range(1, 10):
    cnums = 3 * np.exp(1j * 2 * np.pi * (1 / 39) * n)
    plt.scatter(cnums.real, cnums.imag, label="event", marker="o", color="red", s=200)
    plt.savefig(f'D:\\pic_{n}.png')

If you want to export to gif: you should learn more about ImageMagick, PillowWriter or imageio. Here I example imageio as follows:

import matplotlib.pyplot as plt
import numpy as np
import imageio

fig, ax = plt.subplots()

ax.set(xlim=(-4, 4), ylim=(-4, 4))

red_dot = ax.scatter(None, None, color='red')


data = []


def plot_for_offset(n):
    cnums = 3 * np.exp(1j * 2 * np.pi * (1 / 39) * n)
    data.append([cnums.real, cnums.imag])
    red_dot.set_offsets(data)

    fig.canvas.draw()

    img = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8')
    img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))

    return img


imageio.mimsave(r'F:\red-dot.gif', [plot_for_offset(n) for n in range(1, 40)], fps=1)

This is gif image: enter image description here

1

You could use the very simple Celluloid package for making videos out of matplotlib animations. https://github.com/jwkvam/celluloid

Minimal example:

from matplotlib import pyplot as plt
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig)
for i in range(10):
    plt.plot([i] * 10)
    camera.snap()
animation = camera.animate()
animation.save('output.gif')
Roy Shilkrot
  • 3,079
  • 29
  • 25