Skip to content Skip to sidebar Skip to footer

How To Update Matplotlib Pyplot On Every Iteration

I am using rospy to get data of my robot's position and plot this in real time. This is what I have: self.plot_pose() def plot_pose(self): plt.plot(self.pose[0], self.pose

Solution 1:

This is the same as usual with animations. Try to avoid creating new plots every interation and instead update the old one.

self.point, = plt.plot([],[], 'o', color='green')
self.line, =  plt.plot([],[], ls="-", color='red', lw=2)
plt.show(block=False)
self.plot_pose()

def plot_pose(self):
    self.point.set_data(self.pose[0], self.pose[1])
    self.line.set_data([self.pose[0], self.pose[0] - 0.5*np.cos(self.pose[2])],
                       [self.pose[1], self.pose[1] + 0.5*np.sin(self.pose[2])])

    plt.pause(0.0001)

You may need to adjust the limits of the plot (plt.xlim(), plt.ylim()) if the points are outside of it.


Post a Comment for "How To Update Matplotlib Pyplot On Every Iteration"