提高Matplotlib的数据子集的性能



i有一个小的pyqt4应用程序,该应用程序显示了一个大数据集(100k点x 14通道)。我只想显示128分的时间,然后单击以显示下一个时期。

我的天真方法是在循环中的每个步骤上创建图形,并仅绘制我的数据的一个子集。这导致了相当一秒钟的加载时间,我认为这可能适合此任务。

有什么方法可以改善性能?我是否错过了一些仅绘制数据子集的Matplotlib内置功能?我不介意在应用程序开始时增加加载时间,所以也许我可以绘制所有内容并放大?

编辑:提供了一个简单的运行示例

我的机器上的Took 7.39s to plot 8 samples

导入时间

import matplotlib.pyplot as plt
import numpy as np

plt.ion()
num_channels = 14
num_samples = 1024
data = np.random.rand(num_channels, num_samples)
figure = plt.figure()
start = 0
period = 128
axes = []
for i in range(num_channels):
    axes.append(figure.add_subplot(num_channels, 1, i+1))
end = start+period
x_values = [x for x in range(start, end)]
begin = time.time()
num_plot = 0
for i in range(0, num_samples, period):
    num_plot += 1
    end = start+period
    for i, ax in enumerate(axes):
        ax.hold(False)
        ax.plot(x_values, data[i][start:end], '-')
        ax.set_ylabel(i)
    start += period
    figure.canvas.draw()
print("Took %.2fs to plot %d samples" % (time.time()-begin, num_plot))

使用 @joe-kington从这里开始答案:如何更新matplotlib中的绘图,将性能提高到一个不错的值。

我现在仅使用set_ydata()更改行对象的Y值。调用ax.plot()时返回行对象,该对象仅调用一次。

编辑:添加了一个运行示例: 我的机器上的Took 3.11s to plot 8 samples

import time
import matplotlib.pyplot as plt
import numpy as np

plt.ion()
num_channels = 14
num_samples = 1024
data = np.random.rand(num_channels, num_samples)
figure = plt.figure()
start = 0
period = 128
axes = []
for i in range(num_channels):
    axes.append(figure.add_subplot(num_channels, 1, i+1))
end = start+period
x_values = [x for x in range(start, end)]
lines = []
begin = time.time()
num_plot = 1 # first plot
for i, ax in enumerate(axes):
    ax.hold(False)
    # save the line object
    line, = ax.plot(x_values, data[i][start:end], '-')
    lines.append(line)
    ax.set_xlim([start,end])
    ax.set_ylabel(i)
start += period
for _ in range(period, num_samples, period):
    num_plot += 1
    end = start + period
    for i, line in enumerate(lines):
        line.set_ydata(data[i][start:end])
    start += period
    figure.canvas.draw()
print("Took %.2fs to plot %d samples" % (time.time()-begin, num_plot))

最新更新