plot ros topic in python code

asked 2013-11-17 19:56:56 -0600

canatan gravatar image

updated 2017-03-17 19:45:04 -0600

130s gravatar image

Hi Is it possible to plot values from a ros topic in python ? For example I want to leverage matplotlibs draw function and look at more than one topic as well as add some values of my own on the graph I know I could use rqt plot but that means I have to plot evening outside of my code as well as publish whatever extra values I want to a topic and then use rqt plot to plot those too.

I want to be able to plot stuff in the callback for the topic I subscribed to or something similar so data is updated the second I get it.

For example I subscribed to the odometry topic (msg type nav_msgs/Odometry) and want to plot the x y z of the position. I haven't been able to plot data as it comes.

What I have been able to do is the following: Collect all my data in a vector and then plot it at the end

Here's my plotting function:

  def plot_odom(self):

    x = []
    y = []
    plt.ion()
    plt.show()
    #self.odom.pose.pose.position.x
    print "in plot_odom"
    for odomx in self.odom_vec:
      #print "x ", x
      #print "y ", y
      #raw_input("continue")
      x.append(odomx.header.stamp.to_sec())
      y.append(odomx.pose.pose.position.x)
      plt.plot(x,y,'bo')
      plt.ylabel("odom")
      plt.draw()
      time.sleep(0.05)`

This seems to work okay in terms of giving an animation which is similar to what I would want if this were in the callback function.

I hope I've been able to explain myself well.

edit retag flag offensive close merge delete

Comments

Yes, it is. Just subscribe to it and then draw it. Could you be more specific of what you are looking for in addition?

dornhege gravatar imagedornhege ( 2013-11-17 22:06:26 -0600 )edit

I updated my question. I hope its better explained now. Thanks dornhege

canatan gravatar imagecanatan ( 2013-11-18 03:37:37 -0600 )edit