具有2d数组的Python Matplotlib Hist2d

时间:2014-05-22 10:35:53

标签: python matplotlib histogram

我想通过将两个2D数组作为参数,Tx和alt_array,相同大小(56000,40)来制作二维直方图

def histo_2D(alt, Tx):  
u,v = 56000,40
Tx = np.zeros((u,v))
alt_array = np.zeros((u,v))
alt,tx = np.zeros((v)), np.zeros((v))
for i in range(0,v):
    alt[i] = i
    tx[i] = i
alt_array[:][:] = alt 
Tx[:][:] = tx
    alt_array[:][:] = alt 
    print np.shape(Tx), np.shape(alt_array)
    plt.hist2d(Tx , alt_array)

但是当我尝试执行我的程序时,我收到此错误消息:

Traceback (most recent call last):
  File "goccp.py", line 516, in <module>
    histo_2D(alt,Tx)
  File "goccp.py", line 376, in histo_2D
    plt.hist2d(Tx , alt_array)
  File "/Code/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2847, in hist2d
    weights=weights, cmin=cmin, cmax=cmax, **kwargs)
  File "/Code/anaconda/lib/python2.7/site-packages/matplotlib/axes.py", line 8628, in hist2d
    normed=normed, weights=weights)
  File "/Code/anaconda/lib/python2.7/site-packages/numpy/lib/twodim_base.py", line 650, in histogram2d
    hist, edges = histogramdd([x, y], bins, range, normed, weights)
  File "/Code/anaconda/lib/python2.7/site-packages/numpy/lib/function_base.py", line 288, in histogramdd
    N, D = sample.shape
ValueError: too many values to unpack

我试过使用扁平化阵列,但结果并不是很好......

1 个答案:

答案 0 :(得分:2)

documentation for hist2d州:

  

matplotlib.pyplot.hist2d(x,y,bins = 10,range = None,normed = False,weights = None,cmin = None,cmax = None,hold = None,** kwargs)

     

参数: x,y:array_like,shape(n,):

因此xy需要是一维的;你的价值观是二维的。

还要看一下示例,在文档的最后给出。