0
我一直在嘗試將動畫嵌入到iPython筆記本中,但沒有成功。我在運行10.8.5的Mac上使用最新版本的Enthought Canopy(python 2.7.3),並使用Safari作爲默認瀏覽器。iPython筆記本沒有嵌入動畫
多少失敗的實驗後,我試着在網絡上使用此代碼
%pylab inline
from tempfile import NamedTemporaryFile
VIDEO_TAG = """<video controls>
<source src="data:video/x-m4v;base64,{0}" type="video/mp4">
Your browser does not support the video tag.
</video>"""
def anim_to_html(anim):
if not hasattr(anim, '_encoded_video'):
with NamedTemporaryFile(suffix='.mp4') as f:
anim.save(f.name, fps=20, extra_args=['-vcodec', 'libx264'])
video = open(f.name, "rb").read()
anim._encoded_video = video.encode("base64")
return VIDEO_TAG.format(anim._encoded_video)
from IPython.display import HTML
def display_animation(anim):
plt.close(anim._fig)
return HTML(anim_to_html(anim))
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
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,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
# call our new function to display the animation
display_animation(anim)
從傑克Vanderplas。我安裝了ffmpeg。
在運行代碼時,我得到視頻進度條,但沒有圖形,只是視頻進度條上方的空白空間。
經過幾天的工作,我還沒有找到解決方案(以上是最接近我來)。任何人都可以看到有什麼問題或建議嘗試?
非常感謝。