我在here上读到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发布于 2011-06-14 23:49:09
下采样在这里是一个很好的解决方案--绘制10M点将消耗matplotlib中的大量内存和时间。如果你知道多少内存是可以接受的,那么你可以根据这个大小进行下采样。例如,假设1M点占用额外的23MB内存,您发现它在空间和时间上是可以接受的,因此您应该下采样,使其始终低于1M点:
if(len(a) > 1M):
a = scipy.signal.decimate(a, int(len(a)/1M)+1)
pylab.plot(a)或者像上面的代码片段(上面的代码片段可能对你的口味来说太过激进了)。
发布于 2013-07-16 06:53:56
我也经常对极值感兴趣,所以在绘制大块数据之前,我会这样做:
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当然,np.max只是极限计算函数的一个例子。
附注:使用numpy的“跨步技巧”,应该可以避免在重塑过程中复制数据。
发布于 2019-05-16 06:49:44
我对保留对数采样图的一侧很感兴趣,所以我想出了这个:(下采样是我的第一次尝试)
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()

https://stackoverflow.com/questions/4976303
复制相似问题