5

Is there any plotting option in Python (IPython-Jupyter notebook) which accepts generators?

AFAIK matplotlib doesn't support that. The only option I discovered is plot.ly with their Streaming API, but I would prefer not to use online solution due to big amount of data I need to plot in real-time.

tomasbedrich
  • 1,350
  • 18
  • 26

1 Answers1

8

A fixed length generator can always be converted to a list.

vals_list = list(vals_generator)

This should be appropriate input for matplotlib.


Guessing from your updated information, it might be something like this:

from collections import deque
from matplotlib import pyplot

data_buffer = deque(maxlen=100)
for raw_data in data_stream:
  data_buffer.append(arbitrary_convert_func(raw_data))
  pyplot.plot(data_buffer)

Basically using a deque to have a fixed size buffer of data points.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 1
    Thank you, I know how to create list from generator. But I face the problem of getting the data in real-time. – tomasbedrich Sep 24 '15 at 19:15
  • In this case, you need to be more specific what your problem is. Does the generator already give you the full data at any moment, and you want to update the plot when new data arrives? Does the generator just yield *new* data points and you need to add them to the previous data? In both cases, what is preventing you from looping over the generator and appending/updating the data by hand? Do you actually have to get the data in the first place, in which case what data are you trying to get and from where? – MisterMiyagi Sep 24 '15 at 19:27
  • The generator yields data received from a serial line connected to some hardware measuring acceleration. I have to do some real-time processing on that data (on each sample) and then plot result (and keep the plot updated). – tomasbedrich Sep 24 '15 at 23:46
  • Sound like you should just have a `for new_data in generator`, process the `new_data` (possibly merging it with pre-existing data) and then plot this processed data the usual way. You should add a minimal-working example and specific description of the data flow to your question in order to get helpful answers. – MisterMiyagi Sep 25 '15 at 06:33
  • Okay, I was hoping that it is possibly more convenient way to process new data, than "merging with pre-existing data" and plotting the graph again. – tomasbedrich Sep 25 '15 at 20:56