2015-07-19 46 views
0

我有一個100.000.000樣本數據集,我想用pyplot做一個直方圖。但是閱讀這個大文件會嚴重影響我的記憶力(光標不再移動,...),所以我正在尋找方法'幫助'pyplot.hist。我在想,把文件分成幾個小文件可能會有所幫助。但我不知道如何將它們結合起來。如何更新pyplot直方圖

回答

2

只要每次調用它時都保持固定,您可以合併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') 

enter image description here

+0

我會用'numpy.histogram'函數來計算子直方圖所以沒有必要繪製完成。只需將每次迭代的箱數添加到箱的總數中即可。此外,您可以使用'path.Path'類繪製柱狀圖,如果您有很多分檔,則該柱狀圖比柱狀圖快得多。看[這個例子](http://matplotlib.org/examples/animation/histogram.html)。 – titusjan

+0

@titusjan公平點 – tom