在matplotlib中缓慢保存PDF

时间:2015-03-12 11:12:29

标签: python pdf matplotlib

我有两种不同的粒子集(它是AMUSE类,但与问题或答案没有任何相关性)。第一个较小,包含大约500万个颗粒(~290 MB)。第二个4100万(~2.3 GB)。我希望两者都有一个XY图(带有np.histogram2d()),带有等高线。这一切都像魅力,但将大数据集的数字保存为pdf需要40分钟。当我对第一个数据集执行完全相同的操作时,只需5秒钟。

我用于此的功能如下所示。我有多行打印时间,所以我可以精确定位花费这么长时间的位置,结果是pyplot.savefig()函数。终端输出低于代码。

def hist_plot_x_z_contour(particles, N_reductions = 128):
    time1 = time.time()
    fig= pyplot.figure(figsize=[8,8],dpi=200)
    ax = pyplot.subplot(111)

    extent = [-15,15,-5,5]
    cmap = 'Blues'

    Z, xedges, yedges = np.histogram2d(particles.x.value_in(units.kpc),particles.z.value_in(units.kpc), bins=200, range=[extent[:2],extent[2:]])
    del(particles)
    Z = np.ma.masked_where(Z<= 0, Z)
    pyplot.imshow(Z.T,origin='low',extent=extent,cmap=cmap,interpolation='nearest',aspect='auto',norm=colors.LogNorm())
    print 'Point 1', time.time() - time1

    cb = pyplot.colorbar()  
    cb.set_label('Amount of Particles')

    #Contourlines
    x = 0.5*(xedges[:-1] + xedges[1:])
    y = 0.5*(yedges[:-1] + yedges[1:])
    print 'Point 2', time.time() - time1
    Y, X = np.meshgrid(y, x)
    levels = np.logspace(0,np.log10(Z.max()), 8 )[2:]
    print 'Point 3', time.time() - time1

    pyplot.contour(X,Y,Z,locator=ticker.LogLocator(), colors='k', levels=levels, lw=2,norm=colors.LogNorm(),vmin=1,vmax =Z.max())
    print 'Point 4', time.time() - time1

    pyplot.xlim(extent[:2])
    pyplot.ylim(extent[2:])
    pyplot.xlabel('X ($kpc$)')
    pyplot.ylabel('Z ($kpc$)')


    print 'Point 5', time.time() - time1
    pyplot.savefig(file_location_for_pictures+ 'xz_contour_%d.pdf' %N_reductions)
    print 'Point 6', time.time() - time1
    return

终端输出(大样本):

Point 1 20.2287580967
Point 2 21.1261689663
Point 3 21.245880127
Point 4 21.6766190529
Point 5 21.6768140793
Point 6 2436.35565519

所以很明显,大部分时间是在第5点和第6点之间,大约40分钟。我预计直方图和(在较小程度上)轮廓线将占用大部分时间,但事实并非如此。生成的图像仅为500 kb。我在拥有8GB RAM的计算机上运行它,在看到创建直方图所需的时间之后,这也不是瓶颈。

所以我的问题是,为什么需要这么长时间?

0 个答案:

没有答案
相关问题