如何使用matplotlib/mplot3d绘制3D直方图? [英] How to plot a 3D histogram with matplotlib/mplot3d?

查看:135
本文介绍了如何使用matplotlib/mplot3d绘制3D直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个数组,我正在尝试制作3D直方图.

  x = [1、2、3、2、5、2、6、8、6、7]y = [10,10,20,50,20,20,30,10,40,50,60]z = [105, 25, 26, 74, 39, 85, 74, 153, 52, 98]

这是我目前的尝试:

from mpl_toolkits.mplot3d 导入 Axes3D导入matplotlib.pyplot作为plt将numpy导入为np无花果= plt.figure()ax = plt.axes(projection='3d')binsOne = sorted(set(x))binsTwo = 排序(设置(y))hist,xedges,yedges = np.histogram2d(x,y,bins = [binsOne,binsTwo])xpos,ypos = np.meshgrid(xedges [:-1] + 0.25,yedges [:-1] + 0.25)xpos = xpos.flatten('F')ypos = ypos.flatten('F')zpos = np.zeros_like(xpos)dx = dx.flatten()dy = dy.flatten()dz = hist.flatten()ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')

如何将 z 数组合并到我的 3D 直方图中?

解决方案

z 数组必须具有相同的形状,而不是 x y 但是 xposypos(它们本身的形状相同).您可能会发现

I have three arrays and I am trying to make a 3D histogram.

x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50, 60]
z = [105, 25, 26, 74, 39, 85, 74, 153, 52, 98]

Here's my attempt so far:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')

binsOne = sorted(set(x))
binsTwo = sorted(set(y))
hist, xedges, yedges = np.histogram2d(x, y, bins=[binsOne, binsTwo])
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25 , yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)

dx = dx.flatten()
dy = dy.flatten()
dz = hist.flatten()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')

How do I incorporate the z array into my 3D histogram?

解决方案

The z array must have the same shape not of x and y but of xpos and ypos (which are of themselves the same shape). You may find this example more useful than the one you appear to be drawing from. The following code is to demonstrate the example in the first link applied to your question,

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

_x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
_y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50]
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
_z = np.array([105, 25, 26, 74, 39, 85, 74, 153, 52, 98])

# There may be an easier way to do this, but I am not aware of it
z = np.zeros(len(x))
for i in range(1, len(x)):
    z[i] = _z[(i*len(_z)) / len(x)]

bottom = np.zeros_like(z)
width = depth = 1

ax.bar3d(x, y, bottom, width, depth, z, shade=True)
plt.show()

这篇关于如何使用matplotlib/mplot3d绘制3D直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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