【问题标题】:Python equivalent of Matlab's hist3Python 等价于 Matlab 的 hist3
【发布时间】:2016-08-23 23:28:28
【问题描述】:
for i=1:n
    centersX(:,i)=linspace(min(xData)+dX/2,max(xData)-dX/2,nbins)';
    centersY(:,i)=linspace(min(yData)+dY/2,max(phase)-dY/2,nbins)';

    centers = {centersX(:,i),centersY(:,i)};
    H(:,:,i) = hist3([xData yData],centers);
end

在每次迭代中,我使用linspace 函数构造centersXcentersY。然后我将它们存储在一个名为 centers 的 2x1 单元数组中。 H 是一个 nbins X nbins X n 结构。在每次迭代中,我用 hist3 中的数据填充 H 的 nbins X nbins 切片。

我正在寻找 Python 等价物。我在传递 numpy.histogram2d 的参数时遇到问题:

H[:,:,i] = numpy.histogram2d(xData,yData,centers)

我收到以下错误:

Traceback (most recent call last):
  line 714, in histogramdd
    N, D = sample.shape
AttributeError: 'list' object has no attribute 'shape'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  line 36, in <module>
    H[:,:,i] = numpy.histogram2d(xData, yData, centers)
  line 714, in histogram2d
    hist, edges = histogramdd([x, y], bins, range, normed, weights)
  line 718, in histogramdd
    N, D = sample.shape
ValueError: too many values to unpack (expected 2)

由于 Python 没有元胞数组,我将中心更改为数组数组,其中 centers[0] = centersXcenters[1] = centersY。我需要改变什么,以假设 matlab 和 python 之间的数据相同,输出将匹配?

编辑: 我也尝试过H[:,:,i] = numpy.histogram2d(xData,yData, bins=(centersX,centersY)) 将合并步骤剪切为centers,但没有运气。

【问题讨论】:

  • xDatayDatacentersXcentersY的类型有哪些?
  • @Eric float64。使用 numpy 构造函数后,我没有将它们转换为任何东西
  • 所以他们是ndarrays?什么.shape
  • ndarray.shape (nbins,n)

标签: python matlab numpy histogram


【解决方案1】:

你试过用方括号组合它们吗?

也许你也可以使用matplotlib.pyplot.hist2d

H[:,:,i], *_ = numpy.histogram2d(xData,yData,bins=[centers[0], centers[1]])
H[:,:,i], *_ = matplotlib.pyplot.hist2d(xData,yData,bins=[centers[0], centers[1]])

在两者中,中心的值是 bin 边缘,而不是中心。您必须调整计算。我认为删除 dX/2 就足够了:

centersX(:,i)=linspace(min(xData),max(xData),nbins)';
centersY(:,i)=linspace(min(yData),max(phase),nbins)';

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 2012-10-18
    • 2022-11-02
    • 1970-01-01
    • 2014-10-17
    • 2021-11-25
    • 2014-09-07
    • 2016-11-02
    • 1970-01-01
    相关资源
    最近更新 更多