real - graficas con animacion python
Construyendo una animaciĆ³n usando Python Gizeh (2)
Puedo crear un diagrama simple con formas y números. Estoy usando el siguiente código:
import gizeh as gz
W, H = 500, 300
surface = gz.Surface(W,H, bg_color=(1,0.7,1))
for a in range(1,9):
rect = gz.rectangle(lx = 10, ly = 10, xy=(W/a,H/a), fill =(0,1,0.7))
rect.draw(surface)
txt = gz.text(str(a), fontfamily="Dancing Script", fontsize=15, fill=(0,0,0),xy=(W/a,H/a))
txt.draw(surface)
surface.ipython_display()
También creé una versión usando moviepy:
import numpy as np
import gizeh as gz
import moviepy.editor as mpy
W, H = 500, 300
duration = 5
figpath = ''/tmp/''
fps = 1
def make_frame(t):
surface = gz.Surface(W,H, bg_color=(1,1,1))
rect = gz.rectangle(lx = 10, ly = 10, xy=(W/(t+1),H/2), fill =(0,1,0.7))
rect.draw(surface)
txt = gz.text(str(t+1), fontfamily="Dancing Script", fontsize=15, fill=(0,0,0),xy=(W/(t+1),H/2))
txt.draw(surface)
return surface.get_npimage()
clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_videofile(figpath + ''trax_0.mp4'', fps=fps)
clip.ipython_display(fps=fps, width=W, autoplay=0, loop=0)
Me gustaría poder crear GIF animado usando un retraso de tiempo entre cada paso del ciclo.
Intenta usar MoviePy, un módulo del autor de Gizeh.
Mire un buen artículo donde se usan Gizeh y MoviePy para la animación: http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/
#!/usr/bin/env python3
import numpy as np
import gizeh as gz
import moviepy.editor as mpy
W,H = 128,128 # 128x128 pixel
duration = 2 # 2 seconds
ncircles = 20 # Number of circles
def make_frame(t):
surface = gz.Surface(W,H)
for i in range(ncircles):
angle = 2*np.pi*(1.0*i/ncircles+t/duration)
center = W*( 0.5+ gz.polar2cart(0.1,angle))
circle = gz.circle(r= W*(1.0-1.0*i/ncircles),
xy= center, fill= (i%2,i%2,i%2))
circle.draw(surface)
return surface.get_npimage()
clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_gif("circles.gif",fps=15, opt="OptimizePlus", fuzz=10)