我可以在3d中绘制几个直方图吗? [英] Can I plot several histograms in 3d?

查看:66
本文介绍了我可以在3d中绘制几个直方图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制几个类似于这些条形图的直方图密谋.我已经尝试使用hist返回的数组,但是似乎返回了bin边缘,所以我不能在bar中使用它们.

I'd like to plot several histograms similar to the way thesebar graphs are plotted. I've tried using the arrays returned by hist, but it seems that the bin edges are returned, so I can't use them in bar.

有人有什么建议吗?

推荐答案

如果使用np.histogram预先计算直方图,则会发现hist数组和bin edges . plt.bar期望垃圾箱中心,因此请使用以下公式进行计算:

If you use np.histogram to pre-compute the histogram, as you found you'll get the hist array and the bin edges. plt.bar expects the bin centres, so calculate them with:

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

要改编Matplotlib示例:

To adapt the Matplotlib example:

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()

这篇关于我可以在3d中绘制几个直方图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆