【问题标题】:2D Histogram from a 3x3 array in PYTHONPYTHON 中 3x3 数组的 2D 直方图
【发布时间】:2013-07-09 21:09:00
【问题描述】:

这听起来可能微不足道,但我无法在 PYTHON 中找到解决方案。在 ROOT 或 MATLAB 中没有问题。

所以,我有一个 3x3 数组,我希望数组中的每个元素都代表一个 bin 的高度(频率)。我应该有一个带有 9 个 bin 的直方图。这是我一直在尝试的一个示例。

import numpy as np
import matplotlib.pyplot as plt

H = np.array([[21,33,6],[25,20,2],[80,40,0]])

hist, bin = np.histogramdd(H, bins=3)

center = 0.5*(bin[:-1] + bin[1:])

plt.bar(center, hist)
plt.show()

我已经尝试过 histogram2D,但我找不到任何可以让它与 PYTHON 一起使用。提前感谢您对此提供的任何帮助。

【问题讨论】:

    标签: python-2.7 numpy histogram


    【解决方案1】:

    如果我没记错的话,应该是这样吧:

    H=H.reshape(-1)
    plt.bar(np.arange(H.shape[0]),H)
    

    你也可以做一个 3D 直方图:

    extent = [0,2,0,2]
    plt.imshow(H, extent=extent, interpolation='nearest')
    plt.colorbar()
    plt.show()
    

    3D 柱状图:

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    for z,height in enumerate(H):
        cs = [c] * len(xs)
        cs[0] = 'c'
        ax.bar(np.arange(3), height, zs=z, zdir='y', color=cs, alpha=0.8)
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    plt.show()
    

    以上应该可以,我现在没有笔记本电脑。更多示例可以在here 找到。可以在 here 找到一个很好的 3D 条示例。

    【讨论】:

    • 谢谢,这正是我所需要的。您是否有机会知道我如何将其显示为带有条形的普通 3D 直方图。再次感谢。
    • 再次感谢您的帮助。
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 2020-09-25
    • 2023-03-12
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多