Plotting data from generator in Python

泪湿孤枕 提交于 2021-01-27 16:31:12

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/32768357/plotting-data-from-generator-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!