Robotics StackExchange | Archived questions

plot ros topic in python code

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.

Asked by canatan on 2013-11-17 20:56:56 UTC

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?

Asked by dornhege on 2013-11-17 23:06:26 UTC

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

Asked by canatan on 2013-11-18 04:37:37 UTC

Answers