我有两个不同大小的粒子集(它是一个AMUSE类,但与问题或答案没有任何关联)。第一个更小,包含大约500万个粒子(约290MB)。第二个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
终端输出(大样本):
^{pr2}$所以很明显,大部分时间在第5点到第6点之间,大约40分钟。我原以为直方图和(在较小程度上)轮廓线会占用大部分时间,但事实并非如此。生成的图像只有500kb。我在一台内存为8GB的计算机上运行这个程序,在看到创建直方图所需的时间之后,这也不是瓶颈。在
所以我的问题是,为什么要花这么长时间?在
目前没有回答
相关问题 更多 >
编程相关推荐