Matplotlib animations - how to export them to a format to use in a presentation?

2024/9/22 18:17:35

So, I learned how to make cute little animations in matplotlib. For example, this:

import numpy as np
import matplotlib
import matplotlib.pyplot as pltplt.ion()fig = plt.figure() 
ax  = fig.add_subplot(111) ax.set_xlim(0, 1)
ax.set_ylim(-2,2)dt = 0.01
q  = 0.01
t = np.arange(0,1,dt)
x = np.sin(2*np.pi*t)
line, = ax.plot(t,x, '-')
fig.canvas.draw()
for i in xrange(100):x = (1-q) * x + q* np.random.normal(size = len(t))line.set_ydata(x)fig.canvas.draw()

This works and it's very nice. But how I use this to make a movie I can display, for example, in a pdf presentation? I tried to do fig.savefig("test.gif") but there's an error message indicating that matplotlib doesn't export gifs.

Is there a way of doing this without resorting to external tools (like making a lot of png's and stitching them together)?

Answer

In short, no there is no other way besides using an external tool. There is a new module in 1.1.0 that helps you do animations and save them to mpeg4 format. It uses an external tool to automatically do this conversion, from many frames to a single movie. You can make your gif using imagemagick's convert or use ffmpeg or mencoder, which are the two options provided by the new animation module.

https://en.xdnf.cn/q/71916.html

Related Q&A

Where is python interpreter located in virtualenv?

Where is python intrepreter located in virtual environment ? I am making a GUI project and I stuck while finding the python interpreter in my virtual environment.

Tkinter check which Entry last had focus

I am working on a program that has a virtual keyboard I created using Tkinter. The pages that have the keyboard enabled have entry widgets where the users need to input data. I am using pyautogui as …

Python Popen grep

Id like Popen to execute:grep -i --line-buffered "grave" data/*.txtWhen run from the shell, this gives me the wanted result. If I start, in the very same directory where I test grep, a python…

Url structure and form posts with Flask

In Flask you write the route above the method declaration like so: @app.route(/search/<location>/) def search():return render_template(search.html)However in HTML the form will post to the url in…

How can I simulate a key press in a Python subprocess?

The scenario is, I have a Python script which part of it is to execute an external program using the code below:subprocess.run(["someExternalProgram", "some options"], shell=True)An…

Difference between pd.merge() and dataframe.merge()

Im wondering what the difference is when you merge by pd.merge versus dataframe.merge(), examples below:pd.merge(dataframe1, dataframe2)anddataframe1.merge(dataframe2)

ctypes in python crashes with memset

I am trying to erase password string from memory like it is suggested in here.I wrote that little snippet:import ctypes, sysdef zerome(string):location = id(string) + 20size = sys.getsizeof(string)…

Python __del__ does not work as destructor? [duplicate]

This question already has answers here:What is the __del__ method and how do I call it?(5 answers)Closed 4 years ago.After checking numerous times, I did find inconsistent info about the topic.In some…

How to set default button in PyGTK?

I have very simple window where I have 2 buttons - one for cancel, one for apply. How to set the button for apply as default one? (When I press enter, "apply" button is pressed)However, I wa…

Can Python recognize changes to a file that it is running interactively?

I was doing some troubleshooting and I was curious if it is possible to run a Python script interactively, change a function defined in the script, save the file, then have the interactive shell recogn…