加快Matplotlib?

| 我在这里阅读到matplotlib擅长处理大型数据集。我正在编写一个数据处理应用程序,并将matplotlib图表嵌入到wx中,并且发现matplotlib在处理大量数据方面在速度和内存方面都是很麻烦的。除了降低输入采样率之外,还有谁知道一种加速(减少)matplotlib的方法吗? 为了说明matplotlib对内存有多严重,请考虑以下代码:
import pylab
import numpy
a = numpy.arange(int(1e7)) # only 10,000,000 32-bit integers (~40 Mb in memory)
# watch your system memory now...
pylab.plot(a) # this uses over 230 ADDITIONAL Mb of memory
    
已邀请:
        下采样是一个很好的解决方案-绘制1000万个点会消耗matplotlib中的大量内存和时间。如果知道多少内存可以接受,则可以基于该数量进行下采样。例如,假设1M点占用了23 MB的额外内存,那么您发现它在空间和时间方面是可以接受的,因此您应该降低采样率,使其始终低于1M点:
if(len(a) > 1M):
   a = scipy.signal.decimate(a, int(len(a)/1M)+1)
pylab.plot(a)
或类似上面的代码片段(上面的内容可能会过度降低您的口味。)     
        我也经常对极限值感兴趣,因此在绘制大块数据之前,我以这种方式进行操作:
import numpy as np

s = np.random.normal(size=(1e7,))
decimation_factor = 10 
s = np.max(s.reshape(-1,decimation_factor),axis=1)

# To check the final size
s.shape
当然,ѭ3只是极限计算功能的一个示例。 附言 使用
numpy
“大头把戏”,应该有可能避免在重塑过程中复制数据。     
        我对保留日志采样图的一侧感兴趣,因此我想到了这一点: (降采样是我的第一次尝试)
def downsample(x, y, target_length=1000, preserve_ends=0):
    assert len(x.shape) == 1
    assert len(y.shape) == 1
    data = np.vstack((x, y))
    if preserve_ends > 0:
        l, data, r = np.split(data, (preserve_ends, -preserve_ends), axis=1)
    interval = int(data.shape[1] / target_length) + 1
    data = data[:, ::interval]
    if preserve_ends > 0:
        data = np.concatenate([l, data, r], axis=1)
    return data[0, :], data[1, :]

def geom_ind(stop, num=50):
    geo_num = num
    ind = np.geomspace(1, stop, dtype=int, num=geo_num)
    while len(set(ind)) < num - 1:
        geo_num += 1
        ind = np.geomspace(1, stop, dtype=int, num=geo_num)
    return np.sort(list(set(ind) | {0}))

def log_downsample(x, y, target_length=1000, flip=False):
    assert len(x.shape) == 1
    assert len(y.shape) == 1
    data = np.vstack((x, y))
    if flip:
        data = np.fliplr(data)
    data = data[:, geom_ind(data.shape[1], num=target_length)]
    if flip:
        data = np.fliplr(data)
    return data[0, :], data[1, :]
这使我可以更好地保留情节的一侧:
newx, newy = downsample(x, y, target_length=1000, preserve_ends=50)
newlogx, newlogy = log_downsample(x, y, target_length=1000)
f = plt.figure()
plt.gca().set_yscale(\"log\")
plt.step(x, y, label=\"original\")
plt.step(newx, newy, label=\"downsample\")
plt.step(newlogx, newlogy, label=\"log_downsample\")
plt.legend()
    

要回复问题请先登录注册