用补丁更新matplotlib

2024-05-16 23:30:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我到处都找过了,但似乎什么也找不到。我的用例非常简单:我有一个有位置和方向的机器人。我想把这个位置和方向画在一张图上。但是,我希望机器人能够移动,所以每次T步(让T=1秒)时,我希望图形随着下一个机器人位置更新。你知道吗

按照我实现代码的方式,我生成了一个补丁数组,每个轮子一个,机器人的身体一个。然后,我只想将这三个补丁显示在图形中。我可以初始化补丁,但是当我试图重画它们时,我不能让图形重画它自己。你知道吗

我知道这可能是一个已经回答过的问题,但是我找不到任何适合我的特定代码的解决方案。请帮帮我!你知道吗

def drawInitialize(self):

    #show the board itself
    x_lims = (0, self.width)
    y_lims = (0, self.length)
    self.fig = plt.figure(figsize=(5,5))
    self.ax = self.fig.add_subplot(111, aspect='equal')
    plt.xticks(np.arange(0, self.width+1, int(self.width/5)))
    plt.yticks(np.arange(0, self.length+1, int(self.length/5)))
    plt.xlim(x_lims)
    plt.ylim(y_lims)

    #print the robot (this works!)
    robotPatch = self.getRobotPatch()
    for i in range(len(robotPatch)):
        self.ax.add_patch(robotPatch[i])
        #self.ax.add_artist(robotPatch[i])
    plt.show()

def draw(self):
    self.ax.clear()
    robotPatch = self.getRobotPatch()
    for i in range(len(robotPatch)):
        self.ax.add_patch(robotPatch[i])
    plt.show() #THIS DOESNT WORK!!

#initialize the board, (this calls drawInitialize)
gb = GameBoard(500,750, Robot(300,450,1, wheel_radius=20, axel_length=85.0))

#this should rotate the robot and redraw it
for i in range(5):
    gb.draw() # this SHOULD redraw the robot! but nothing happens
    gb.robot.theta = gb.robot.theta + math.pi/2
    time.sleep(1)

Tags: theselfadd图形show机器人robotplt