Error with saving animation

Partha Pratim Ghosh ghosh.parthapratim.ukzn at gmail.com
Sat Oct 11 06:55:19 CEST 2014


Dear All,

   I am having a problem with saving the output of an animation as a file. I
provide here two listing, first the small program which otherwise works
well, except while executing the last few lines to save the output produces
error which I also provide:

1. the program test.py :

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


fig = plt.figure()          
plt.axis([-1,1,-1,1])       
abcissa, = plt.plot([],[], color='black')
                            
graph, = plt.plot([], [], color='blue')
                            
t, = plt.plot([],[], color='red')

def init():
    ''' set the basic graph and put slate clean for blit'''
    x = np.arange(-1,1,0.01)# the x-values
    y = np.sin(np.pi/2 * x)

    abcissa.set_data(x, 0)  
    graph.set_data(x, y)    

    return abcissa, graph

def animate(i):
    p = -1 + i*0.02   
    tx = [p-1,p+1]
    ty = [np.sin(np.pi/2 * p) - np.pi/2 * np.cos(np.pi/2 *p),
          np.sin(np.pi/2 * p) + np.pi/2 * np.cos(np.pi/2 *p)]
    t.set_data(tx, ty)

    return t,
    
    

ani = anim.FuncAnimation(fig, animate, init_func=init,
                         frames=100, interval=30, blit=True)


plt.show()
Writer = anim.writers['ffmpeg']
writer = Writer(fps=15, bitrate=1800)

ani.save('test.mp4', writer=writer)

2. Error output

    ani.save('test.mp4', writer=writer)
  File "/usr/lib/python3/dist-packages/matplotlib/animation.py", line 719,
in save
    writer.grab_frame(**savefig_kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/animation.py", line 205,
in grab_frame
    dpi=self.dpi, **savefig_kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 1422, in
savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/backend_bases.py", line
2225, in print_figure
    self.figure.dpi = origDPI
  File "/usr/lib/python3/dist-packages/matplotlib/figure.py", line 385, in
_set_dpi
    self.dpi_scale_trans.clear().scale(dpi, dpi)
  File "/usr/lib/python3/dist-packages/matplotlib/transforms.py", line 1786,
in clear
    self._mtx = np.identity(3)
  File "/usr/lib/python3/dist-packages/numpy/core/numeric.py", line 2053, in
identity
    from numpy import eye
  File "<frozen importlib._bootstrap>", line 2280, in _handle_fromlist
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0:
invalid start byte
'
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/idlelib/run.py", line 121, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "/usr/lib/python3.4/queue.py", line 175, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/matplotlib/backends/tkagg.py", line
13, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode,
id(bbox_array))
_tkinter.TclError: this isn't a Tk application

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
    return self.func(*args)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 585, in callit
    func(*args)
  File
"/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line
363, in idle_draw
    self.draw()
  File
"/usr/lib/python3/dist-packages/matplotlib/backends/backend_tkagg.py", line
349, in draw
    tkagg.blit(self._tkphoto, self.renderer._renderer, colormode=2)
  File "/usr/lib/python3/dist-packages/matplotlib/backends/tkagg.py", line
20, in blit
    tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode,
id(bbox_array))
_tkinter.TclError: this isn't a Tk application


Please can someone help me out of this problem.

With my regards,

partha




More information about the Python-list mailing list