0
我有一个100.000.000样本数据集,我想用pyplot做一个直方图。但是阅读这个大文件会严重影响我的记忆力(光标不再移动,...),所以我正在寻找方法'帮助'pyplot.hist
。我在想,把文件分成几个小文件可能会有所帮助。但我不知道如何将它们结合起来。如何更新pyplot直方图
我有一个100.000.000样本数据集,我想用pyplot做一个直方图。但是阅读这个大文件会严重影响我的记忆力(光标不再移动,...),所以我正在寻找方法'帮助'pyplot.hist
。我在想,把文件分成几个小文件可能会有所帮助。但我不知道如何将它们结合起来。如何更新pyplot直方图
只要每次调用它时都保持固定,您可以合并pyplot.hist
的输出或@titusjan建议的numpy.histogram
。例如:
import matplotlib.pyplot as plt
import numpy as np
# Generate some fake data
data=np.random.rand(1000)
# The fixed bins (change depending on your data)
bins=np.arange(0,1.1,0.1)
sub_hist = [], []
# Split into 10 sub histograms
for i in np.arange(0,1000,10):
sub_hist_temp, bins_out = np.histogram(data[i:i+10],bins=bins)
sub_hist.append(sub_hist_temp)
# Sum the histograms
hist_sum = np.array(sub_hist).sum(axis=0)
# Plot the new summed data, using plt.bar
fig=plt.figure()
ax1=fig.add_subplot(211)
ax1.bar(bins[:-1],hist_sum,width=0.1) # Change width depending on your bins
# Plot the histogram of all data to check
ax2=fig.add_subplot(212)
hist_all, bins_out, patches = all=ax2.hist(data,bins=bins)
fig.savefig('histsplit.png')
我会用'numpy.histogram'函数来计算子直方图所以没有必要绘制完成。只需将每次迭代的箱数添加到箱的总数中即可。此外,您可以使用'path.Path'类绘制柱状图,如果您有很多分档,则该柱状图比柱状图快得多。看[这个例子](http://matplotlib.org/examples/animation/histogram.html)。 – titusjan
@titusjan公平点 – tom