【问题标题】:Adjusting the range of x axis for a live plot (serial data stream) in matplotlib在 matplotlib 中调整实时图(串行数据流)的 x 轴范围
【发布时间】:2011-06-17 14:57:20
【问题描述】:

我一直在使用 matplotlib 示例页面上的这段代码。我试图让 x 轴保持给定的窗口。例如,画布将从 x = 0 到 30、1 到 31、2 到 32 绘制。现在我的 x 继续增长。我无法定义设置的窗口大小。谁能指出我正确的方向。

从我的试验来看,无论 x 的值如何,y 都需要具有相同的长度。对于我的程序来说,我只想绘制一个串行数据流。我要走这条路了吗?

import time
import matplotlib
matplotlib.use('TkAgg') # do this before importing pylab
import matplotlib.pyplot as plt
import random

fig = plt.figure()
ax = fig.add_subplot(111)

y = []

def animate():

    while(1):
        data = random.random()
        y.append(data)
        x = range(len(y))

        line, = ax.plot(y)
        line.set_ydata(y)
        fig.canvas.draw()

win = fig.canvas.manager.window
fig.canvas.manager.window.after(100, animate)
plt.show()

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您还可以同时更改 x 和 y 数据,然后更新绘图限制。我不知道您打算运行多长时间,但您可能应该考虑在某个时候转储不需要的 y 数据。

    import matplotlib
    matplotlib.use('TkAgg') # do this before importing pylab
    import matplotlib.pyplot as plt
    import random
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    x = range(30)
    y = [random.random() for i in x]
    line, = ax.plot(x,y)
    
    def animate(*args):
        n = len(y)
        for 1:
            data = random.random()
            y.append(data)
    
            n += 1
            line.set_data(range(n-30, n), y[-30:])
            ax.set_xlim(n-31, n-1)
            fig.canvas.draw()
    
    fig.canvas.manager.window.after(100, animate)
    plt.show()
    

    【讨论】:

    • 这很棒,非常接近我想要的最终产品。我打算一次运行大约 7 小时,并且可能会每小时转储一次。非常感谢您的帮助。干杯!
    • 你生成数字快吗?您可能希望更频繁地执行此操作。
    【解决方案2】:

    这是一个简单的版本,它显示了y的最后30个点(实际上它只是丢弃了除了最后30个点之外的所有数据,因为听起来你不需要存储它),但是x轴标签仍然存在永远在 0-30,这可能不是你想要的:

    def animate(y, x_window):
        while(1):
            data = random.random()
            y.append(data)
            if len(y)>x_window:  
                y = y[-x_window:]
            x = range(len(y))
            ax.clear()
            line, = ax.plot(y)
            line.set_ydata(y)
            fig.canvas.draw()
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    y = []
    win = fig.canvas.manager.window
    fig.canvas.manager.window.after(100, animate(y,30))
    

    所以我添加了一个偏移变量来跟踪我们已经切断了多少 y,然后使用 set_xticklabels 将该数字添加到所有 x 轴标签:

    def animate(y, x_window):
        offset = 0
        while(1):
            data = random.random()
            y.append(data)
            if len(y)>x_window:  
                offset += 1
                y = y[-x_window:]
            x = range(len(y))
            ax.clear()
            line, = ax.plot(y)
            line.set_ydata(y)
            ax.set_xticklabels(ax.get_xticks()+offset)
            fig.canvas.draw()
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    y = []
    win = fig.canvas.manager.window
    fig.canvas.manager.window.after(100, animate(y,30))
    

    这行得通吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多