【问题标题】:Can I plot several histograms in 3d?我可以在 3d 中绘制多个直方图吗?
【发布时间】:2016-02-04 19:51:56
【问题描述】:

我想绘制几个直方图,类似于绘制thesebar 图的方式。我试过使用hist返回的数组,但似乎返回了bin边缘,所以我不能在bar中使用它们。

有人有什么建议吗?

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    如果您使用np.histogram 预先计算直方图,您会发现您将获得hist 数组和bin edgesplt.bar 期望 bin 中心,因此计算它们:

    xs = (bins[:-1] + bins[1:])/2
    

    修改 Matplotlib 示例:

    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')
    nbins = 50
    for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
        ys = np.random.normal(loc=10, scale=10, size=2000)
    
        hist, bins = np.histogram(ys, bins=nbins)
        xs = (bins[:-1] + bins[1:])/2
    
        ax.bar(xs, hist, zs=z, zdir='y', color=c, ec=c, alpha=0.8)
    
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    plt.show()
    

    【讨论】:

    • 嗨@xnx,例如,如何使上面的代码适应箱线图?我因包含“zs”和“zdir”等参数而出错。
    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 2015-07-12
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多