【问题标题】:Python Matplotlib Hist2d with 2d array带有二维数组的 Python Matplotlib Hist2d
【发布时间】:2014-05-22 10:35:52
【问题描述】:

我想通过将两个二维数组作为参数,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

我尝试过使用扁平化数组,但结果并不是很好......

【问题讨论】:

  • 我可以有一个可运行的样本吗?你为什么要避免给我们print 的输出?
  • 因为我认为它没用,它会打印 (56205, 40) (56205, 40)
  • 我编辑了一个可运行的版本,错误值但仍然是同样的问题

标签: python matplotlib histogram


【解决方案1】:

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 需要是一维的;你的价值观是二维的。

也请查看文档末尾给出的示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2012-05-14
    • 2013-06-14
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多