解决办法:
!apt install xvfb -y
!pip install pyvirtualdisplay
!pip install piglet
import gym
import numpy as np
import matplotlib.pyplot as plt
from IPython import display as ipythondisplay
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1400, 900))
display.start()
env = gym.make('CartPole-v0')
for i_episode in range(20):
observation = env.reset()
for t in range(100):
plt.imshow(env.render(mode='rgb_array'))# CHANGED
ipythondisplay.clear_output(wait=True) # ADDED
ipythondisplay.display(plt.gcf()) # ADDED
print(observation)
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
print("Episode finished after {} timesteps".format(t+1))
break
运行结果