1

I receive a bunch of live data from microphones. The length of a data set is 4000 and I receive it 5 times per second.

I use python2.7 and matplotlib. To get the idea of the data sets:

If its quiet

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -2, 0, -1, 0, 0, 0, -1, 1, -2, 0, -2, -2, -8, -1, -1, 0, -2, -1, -2, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1, -1, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, -1,-1, 0, -1, -4, -1, 0, -1, 2, -1, 0, 2, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, ...]

If its loud:

[-372, -37, -157, -143, 93, -212, 304, -225, 432, -177, 450, -79, 351, 40, 159, 147, -77, 213, -296, 222, -440, 173, -467, 77, -366, -41, -161, -147, 90, -215, 308, -225, 435, -175, 446, -77, 351, 42, 167, 147, -70, 210, -296,  -166, 456, -76, 374, 34, ...]

My python script:

def plot_data(data):
    x = [i for i in range(0, 4000)]
    plt.plot(x,data.data)
    plt.show()

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber('/miro/rob01/platform/mics', platform_mics, show_data)
    rospy.spin()

if __name__ == '__main__':
    listener()

I'd like to plot the data and redraw or update every time I get a new data object. Need a continuous update of the graph.

The graph should look like this: enter image description here

Do you know a good way to do so?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Ludwig k.
  • 11
  • 2
  • Side note: In Python 2.7 you should use `xrange` instead of `range` for your `for` loops. In this case you should also be able to just write `x=range(4000)` – user8408080 Feb 06 '19 at 14:11
  • At first read this sounds like something you'd use Bokeh or Plotly for. matplotlib is fairly limited. – Matthew Arthur Feb 06 '19 at 14:17
  • Possible duplicate of [How do I plot in real-time in a while loop using matplotlib?](https://stackoverflow.com/questions/11874767/how-do-i-plot-in-real-time-in-a-while-loop-using-matplotlib) – Diziet Asahi Feb 06 '19 at 14:42
  • I would like to strongly oppose the comment by @MatthewArthur. Matplotlib has limitations in certain areas, which is mainly 3D plotting and web integration, but none of those are required here. Concerning the question, is `show_data` and `plot_data` meant to be the same? Did you look at similar questions on live plotting with matplotlib? I how far do they not help? – ImportanceOfBeingErnest Feb 06 '19 at 15:07

1 Answers1

0

One thing that may work is the animation functionality provided by matplotlib. They have a number of examples. I think this example looks like a simple place to start. Essentially instead of re-plotting every time, you update the data of a Line2D object. I have not tried this with ROS callbacks though so I can't speak to how well it will work in your use-case.

I would really suggest you use rqt_plot if you're already using ROS. It's designed for exactly what you want, real-time plotting of data coming over topics, and you can use matplotlib as the plotting backend (although I think they use pyqtgraph by default which has some speed benefits). It's not even something you have to write code for, it's just a command-line call

rqt_plot

and a GUI will pop up that lets you specify the topic you want to visualize.

As a last suggestion, in case you have mulitple topics you want to plot, I've found Plot Juggler to be a nicer alternative and it has the added advantage that you can use it outside of ROS and also visualize data loaded statically from files.

adamconkey
  • 4,104
  • 5
  • 32
  • 61
  • I'll going to take a look as soon as possible. Unfortunately I'm extremly busy for the next weeks. Thanks for the answers so far. – Ludwig k. Feb 12 '19 at 12:25