1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter
import gymnasium as gym
env = gym.make("MountainCarContinuous-v0", render_mode="rgb_array", goal_velocity=0.1)
obs, _ = env.reset(seed=123, options={"low": -0.7, "high": -0.5})
frames = []
for _ in range(200):
action = env.action_space.sample()
obs, reward, done, _, _ = env.step(action)
frame = env.render()
frames.append(frame)
if done:
break
fig, ax = plt.subplots()
im = ax.imshow(frames[0])
def update(frame):
im.set_array(frame)
return [im]
ani = FuncAnimation(fig, update, frames=frames, interval=5, blit=True)
plt.axis('off')
ani.save("mountaincar_animation.gif", writer=PillowWriter(fps=30))
plt.show()
|