Created
December 26, 2016 22:05
-
-
Save grapefroot/bbcd917be5278b53086c0240f6308e42 to your computer and use it in GitHub Desktop.
This file contains 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 IPython.display import display | |
from JSAnimation.IPython_display import display_animation | |
from matplotlib import animation | |
def run_env_steps(env, num_steps=1000): | |
env.reset() | |
frames = [] | |
for _ in range(num_steps): | |
# Render into buffer. | |
# You will still see the window. | |
frames.append(env.render(mode = 'rgb_array')) | |
action = env.action_space.sample() | |
observation, reward, done, info = env.step(action) | |
if done: | |
break | |
env.render(close=True) | |
return frames | |
def display_frames_as_gif(frames): | |
""" | |
Displays a list of frames as a gif, with controls | |
""" | |
plt.figure(figsize=(frames[0].shape[1] / 72.0, frames[0].shape[0] / 72.0), dpi = 72) | |
patch = plt.imshow(frames[0]) | |
plt.axis('off') | |
def animate(i): | |
patch.set_data(frames[i]) | |
anim = animation.FuncAnimation(plt.gcf(), animate, frames = len(frames), interval=50) | |
display(display_animation(anim, default_mode='loop')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment