Skip to content

Instantly share code, notes, and snippets.

@grapefroot
Created December 26, 2016 22:05
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 grapefroot/bbcd917be5278b53086c0240f6308e42 to your computer and use it in GitHub Desktop.
Save grapefroot/bbcd917be5278b53086c0240f6308e42 to your computer and use it in GitHub Desktop.
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