在Python中绘制来自生成器的数据

tom*_*ich 3 python ipython jupyter

Python(IPython-Jupyter笔记本)中是否有接受生成器的绘图选项?

AFAIK matplotlib不支持这一点.我发现的唯一选项是plot.ly和他们的Streaming API,但我不想使用在线解决方案,因为我需要实时绘制大量数据.

Mis*_*agi 7

固定长度的生成器始终可以转换为列表.

vals_list = list(vals_generator)
Run Code Online (Sandbox Code Playgroud)

这应该是适当的输入matplotlib.


根据您的更新信息猜测,它可能是这样的:

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)
Run Code Online (Sandbox Code Playgroud)

基本上使用deque来拥有固定大小的数据点缓冲区.