Skip to content

Instantly share code, notes, and snippets.

@wookayin
Created January 25, 2019 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wookayin/4b1043c3e0ad09567f46279ae0482a72 to your computer and use it in GitHub Desktop.
Save wookayin/4b1043c3e0ad09567f46279ae0482a72 to your computer and use it in GitHub Desktop.
Matplotlib animations
from matplotlib import animation
from IPython.display import display_html
from IPython.display import HTML
def display_frames_as_gif(frames):
"""
Displays a list of frames as a gif, with controls
"""
fig, ax = plt.subplots()
patch = ax.imshow(frames[0])
ax.axis('on')
def animate(i):
patch.set_data(frames[i])
fps = 30
anim = animation.FuncAnimation(plt.gcf(), animate, frames=len(frames), interval=1000./fps)
#display_html(HTML(anim.to_jshtml(fps=30)))
display_html(HTML(anim.to_html5_video()))
import gym
env = gym.make('SeaquestNoFrameskip-v4')
s = env.reset()
cum_reward = 0
frames = []
for t in range(1000):
frames.append(env.render(mode='rgb_array'))
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
break
display_frames_as_gif(frames);
plt.close()
@wookayin
Copy link
Author

matplotlib.rc('animation', html='html5')

Then just anim can work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment