Verbesserte Animation mit matplotlib
Die Animation mit matplotlib fand ich toll, aber jetzt habe ich herausgefunden dass es noch besser geht. Mit FuncAnimation erzeugt matplotlib direkt ein animiertes GIF!

Das Script dazu
Ist nicht viel anders als die erste Version, aber es schreibt die Animation direkt in ein File. Das Zusammensetzen der Bilder ist nicht mehr nötig.
# Import the libraries import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import math # Class for 2D points class Point: def __init__( self, x, y ): self.x = x self.y = y # Computes a list of 400 points on the cirumference of a circle def init(): rot = 2.199114858 points = [] delta = math.pi / 100 for i in range(200): points.append( Point(math.sin(i*delta+rot)*350,math.cos(i*delta+rot)*350) ) return points # Initialize the list of points points = init() # Defines the canvas fig, ax = plt.subplots( figsize=(5,5) ) ax.axis( [-400,400,-400,400] ) ax.axis( "off" ) # Draws the figure (incrementally) f = 60 def draw( i ): i += 1 # Draws a line from points[i] to points[2*i] ax.plot( [points[i].x,points[f*i % 200].x], [points[i].y,points[i*f % 200].y] ) # Write the animation directly into a file ani = FuncAnimation( fig, draw, frames=60, interval=1 ) ani.save( "animation.gif", writer="pillow", fps=10, dpi=100 )
Unschön
Das Script funktioniert zwar tadellos, produziert aber ein File mit 862kB. Das fand ich doch sehr gross, vor allem im Vergleich mit der ersten Version.
Ein Online-Dienst schrumpfte das File auf ca. 190kB was schon viel vernünftiger ist.
Im Internet wurde für solche Fälle gifsicle empfohlen, und siehe da: das File ist jetzt nur noch 53kB gross.
gifsicle -O3 --colors 60 animation.gif > matplotlib.gif