有效地从大数据集创建二维直方图

2024-03-29 01:39:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python从HDF5文件中存储的大数据集(100000多个样本)创建2d直方图。我想出了以下代码:

import sys
import h5py
import numpy as np
import matplotlib as mpl
import matplotlib.pylab

f = h5py.File(sys.argv[1], 'r')

A = f['A']
T = f['T']

at_hist, xedges, yedges = np.histogram2d(T, A, bins=500)
extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]

fig = mpl.pylab.figure()
at_plot = fig.add_subplot(111)

at_plot.imshow(at_hist, extent=extent, origin='lower', aspect='auto')

mpl.pylab.show()

f.close()

执行大约需要15秒(100000个数据点)。然而,CERN的根(使用它自己的树数据结构而不是HDF5)可以在不到1s的时间内完成这项工作。你知道我如何加快代码的速度吗?如果有帮助的话,我还可以更改HDF5数据的结构。


Tags: 数据代码importmatplotlibasnpsysextent