Skip to content

Instantly share code, notes, and snippets.

@andrewschreiber
Last active December 29, 2021 12:02
Show Gist options
  • Save andrewschreiber/e9a21bba9c604a790e605fbacd3f4109 to your computer and use it in GitHub Desktop.
Save andrewschreiber/e9a21bba9c604a790e605fbacd3f4109 to your computer and use it in GitHub Desktop.
How to stream OpenAI Gym environment rendering within a Jupyter Notebook

Open jupyter with

$ xvfb-run -s "-screen 0 1400x900x24" jupyter notebook

In Jupyter

import matplotlib.pyplot as plt
%matplotlib inline

After each step

def show_state(env, step=0):
    plt.figure(3)
    plt.clf()
    plt.imshow(env.render(mode='rgb_array'))
    plt.title("%s. Step: %d" % (env._spec.id,step))
    
    plt.pause(0.001)  # pause for plots to update

    if is_ipython:
        display.clear_output(wait=True)
        display.display(plt.gcf())
@baek-jinoo
Copy link

not doing display.display(plt.gcf()) made it much better to view.

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