Created
January 25, 2019 00:41
-
-
Save wookayin/4b1043c3e0ad09567f46279ae0482a72 to your computer and use it in GitHub Desktop.
Matplotlib animations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
matplotlib.rc('animation', html='html5')
Then just
anim
can work.