Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting data from generator in Python

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.

like image 458
tomasbedrich Avatar asked Mar 15 '23 16:03

tomasbedrich


1 Answers

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.

like image 82
MisterMiyagi Avatar answered Mar 23 '23 18:03

MisterMiyagi