MatPlotLib动态时间轴 [英] MatPlotLib dynamic time axis

查看:125
本文介绍了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动态时间轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆