【问题标题】:Animate data by scrolling in Matplotlib通过在 Matplotlib 中滚动来动画数据
【发布时间】:2016-11-24 04:23:18
【问题描述】:

我有一个大型数据集 (~30GB),我想通过查看它滚动过去来对其进行可视化。一个很好的例子是this video. 中的顶部图 我的数据来自 CSV 文件。

到目前为止,我尝试将大量 CSV 文件导入到一个 numpy 数组中,并使用np.roll() 从右侧(如视频中)反复移入一个新列,直到我点击数组的最后一列(通过在mpl.animation.FuncAnimation 迭代中调用np.roll()。 这需要大量的 CPU 和大量的内存。

关于如何解决这个问题的任何建议?我在网上找不到很多可以帮助我解决此问题的示例。

【问题讨论】:

  • 请提供您的代码和 csv 文件的片段。
  • 我不相信我的代码会有帮助。我正在寻找有关如何处理它的建议。也许甚至不使用代码。我只知道尝试加载巨大的数组并尝试为它们设置动画效果不佳(非常慢)。如何做到这一点的伪代码就足够了。

标签: python csv numpy matplotlib


【解决方案1】:

这是 mat plot lib 教程中的一些代码。

import numpy as np
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import math



class Scope(object):
    def __init__(self, ax, maxt=2, dt=0.02):
        self.ax = ax
        self.dt = dt
        self.maxt = maxt
        self.tdata = [0]
        self.ydata = [0]
        self.line = Line2D(self.tdata, self.ydata)
        self.ax.add_line(self.line)
        self.ax.set_ylim(-.1, 1.1)
        self.ax.set_xlim(0, self.maxt)

    def update(self, y):
        lastt = self.tdata[-1]
        if lastt > self.tdata[0] + self.maxt:  # reset the arrays
            self.tdata = [self.tdata[-1]]
            self.ydata = [self.ydata[-1]]
            self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
            self.ax.figure.canvas.draw()

        t = self.tdata[-1] + self.dt
        self.tdata.append(t)
        self.ydata.append(y)
        self.line.set_data(self.tdata, self.ydata)
        return self.line,


def emitter(x=0):
    'return a random value with probability p, else 0'

    while True:
        if x<361:
            x = x + 1
            yield math.sin(math.radians(x))
        else:
            x=0
            x =x + 1
            yield math.sin(math.radians(x))

# Fixing random state for reproducibility
np.random.seed(19680801)


fig, ax = plt.subplots()
scope = Scope(ax)

# pass a generator in "emitter" to produce data for the update func
ani = animation.FuncAnimation(fig, scope.update, emitter, interval=10,
                              blit=True)

plt.show()

我的建议是构建一个生成器,该生成器每次调用它时都会生成您想要显示的下一个数据集。这样您就不需要将整个文件加载到内存中。更多关于here。用将从您的文件中提取的生成器替换 emitter 函数。这样做的缺点是我不相信剧情中会提供完整的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多