MatPlotLib动态时间轴

编程入门 行业动态 更新时间:2024-10-28 11:31:46
本文介绍了MatPlotLib动态时间轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在研究,但没有找到我正在寻找的解决方案.

I've been researching for a bit and haven't found the solution I'm looking for.

有没有办法用 matplotlib 创建动态 x 轴?我有一个正在绘制数据流的图形,我希望 x 轴显示经过的时间(当前是静态的 0-100,而图形的其余部分更新为我的数据流).

Is there a method to creating a dynamic x-axis with matplotlib? I have a graph with a data stream being plotted, and I would like to have the x-axis display elapsed time (currently is static 0-100, while the rest of the graph updating to my data stream).

理想情况下,每个刻度间隔 0.5 秒,并显示最近的 10 秒.该程序将以24/7运行,因此我可以将其设置为实际时间而不是秒表时间.我在研究中只发现了静态的日期时间轴.

Each tick would ideally be .5 seconds apart, with the latest 10s displaying. The program will be running 24/7, so I could have it set to actual time instead of stopwatch time. I've only been finding static datetime axis in my research.

如有必要,我可以提供代码,但对于这个问题似乎没有必要.

I can provide code if necessary, but doesn't seem necessary for this question.

推荐答案

由于我不知道您在流式传输什么,所以我写了一个通用示例,它可能会帮助您解决问题.

Since I don't know what you are streaming, I wrote a generic example and it may help you solving your problem.

from pylab import * import matplotlib.animation as animation class Monitor(object): """ This is supposed to be the class that will capture the data from whatever you are doing. """ def __init__(self,N): self._t = linspace(0,100,N) self._data = self._t*0 def captureNewDataPoint(self): """ The function that should be modified to capture the data according to your needs """ return 2.0*rand()-1.0 def updataData(self): while True: self._data[:] = roll(self._data,-1) self._data[-1] = self.captureNewDataPoint() yield self._data class StreamingDisplay(object): def __init__(self): self._fig = figure() self._ax = self._fig.add_subplot(111) def set_labels(self,xlabel,ylabel): self._ax.set_xlabel(xlabel) self._ax.set_ylabel(ylabel) def set_lims(self,xlim,ylim): self._ax.set_xlim(xlim) self._ax.set_ylim(ylim) def plot(self,monitor): self._line, = (self._ax.plot(monitor._t,monitor._data)) def update(self,data): self._line.set_ydata(data) return self._line # Main if __name__ == '__main__': m = Monitor(100) sd = StreamingDisplay() sd.plot(m) sd.set_lims((0,100),(-1,1)) ani = animation.FuncAnimation(sd._fig, sd.update, m.updataData, interval=500) # interval is in ms plt.show()

希望有帮助

更多推荐

MatPlotLib动态时间轴

本文发布于:2023-11-22 09:33:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616903.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   动态   MatPlotLib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!