Tôi đang cố gắng lưu hoạt ảnh matplotlib đơn giản từ Jake Vanderplas, nhưng tôi vẫn nhận được OSError: [Errno 13] Permission denied
.Không thể lưu hoạt ảnh matplotlib với ffmpeg
Tôi nên lưu ý rằng tôi đã thực hiện hai sửa đổi nhỏ cho ví dụ của Jake Vanderplas. Tôi đã cài đặt ffmpeg từ MacPorts vì vậy tôi đã thêm dòng plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin'
và tôi gặp sự cố được thảo luận trong (Using FFmpeg and IPython), vì vậy tôi đã thêm FFwriter = animation.FFMpegWriter()
.
Đây là mã:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin'
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
FFwriter = animation.FFMpegWriter()
anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
Đây là traceback:
File "ani_debug.py", line 34, in <module>
anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site- packages/matplotlib/animation.py", line 712, in save
with writer.saving(self._fig, filename, dpi):
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 169, in saving
self.setup(*args)
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 159, in setup
self._run()
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 186, in _run
stdin=subprocess.PIPE)
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
Tôi cũng đã cố gắng sử dụng python built-in Spyder và nhận được một traceback tương tự. Bất kỳ đề xuất?
EDIT: Tôi nhận ra rằng tôi đã không đưa ra đường dẫn phù hợp để ffmpeg. Rõ ràng, plt.rcParams['animation.ffmpeg_path']
không hoạt động tương tự như PYTHONPATH
. Bạn phải nói cho mô-đun hoạt hình chính xác nơi ffmpeg là với plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin/ffmpeg'
.
Bây giờ, tôi nhận được một tệp phim sẽ phát, nhưng nội dung bị cắt xén hoàn toàn. Tôi không thể nói những gì tôi đang nhìn.
Đây là traceback:
Exception in Tkinter callback
Traceback (most recent call last):
File "Tkinter.pyc", line 1470, in __call__
File "Tkinter.pyc", line 531, in callit
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/backends/backend_tkagg.py", line 141, in _on_timer
TimerBase._on_timer(self)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/backend_bases.py", line 1203, in _on_timer
ret = func(*args, **kwargs)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 876, in _step
still_going = Animation._step(self, *args)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 735, in _step
self._draw_next_frame(framedata, self._blit)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 753, in _draw_next_frame
self._pre_draw(framedata, blit)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 766, in _pre_draw
self._blit_clear(self._drawn_artists, self._blit_cache)
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 806, in _blit_clear
a.figure.canvas.restore_region(bg_cache[a])
KeyError: <matplotlib.axes.AxesSubplot object at 0x104cfb150>
EDIT: Đối với một số lý do, tất cả mọi thứ đang làm việc tốt ngay bây giờ. Tôi đã thử mọi thứ trên máy tính ở nhà và máy tính công việc của mình, và không ai có thể tạo lại tệp video bị cắt xén mà tôi nhận được sau khi đã khắc phục sự cố đường dẫn ffmpeg.
EDIT: Aaaahaaa! Tôi theo dõi sucker này xuống. Đôi khi tôi sẽ nhập một mô-đun có plt.rcParams['savefig.bbox'] = 'tight'
trong đó. (Tôi sẽ không bao giờ sử dụng mô-đun đó, nhưng rcParams vẫn tồn tại, cho đến khi bạn khởi động lại trình thông dịch python của bạn.) Cài đặt đó làm cho video bị xóa hết. Tôi sẽ đăng giải pháp của tôi dưới đây.
Đợi .. bây giờ bạn sẽ nhận được một truy xuất ngược nhưng cũng nhận được kết quả đầu ra? – KobeJohn
Vâng, crud. Tôi chỉ cố gắng mã của tôi ở trên (với con đường thích hợp để ffmpeg) trên máy tính làm việc của tôi, và nó đã làm việc tốt. Tất cả các thử nghiệm của tôi trước khi điều này đã được trên máy tính ở nhà của tôi. Có lẽ tôi cần phải cài đặt lại ffmpeg trên máy tính ở nhà của tôi. – Stretch