4

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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Matt-pow
  • 946
  • 4
  • 18
  • 31
  • I advice you this post: https://stackoverflow.com/questions/53611716/wrong-overlab-in-bar3d-plot Matplotlib have some issues with the 3D plotting, so pay attention! – Alessandro Peca Jan 03 '19 at 08:25

1 Answers1

1

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

enter image description here

William Miller
  • 9,839
  • 3
  • 25
  • 46
  • Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z? – Matt-pow Jan 03 '19 at 06:04
  • @Matt-pow I don't understand your question, the `z` values in the array are determining the height of the the bar, the `x` values determine the `x` position and the `y` values determine the `y` position. For `x` and `y` pairs that correspond to a common `z` value you see multiple bars. The values on the `y` and `x` axis are a little confusing since the bar `1` in `x` actually goes from `1` to `2` and `8` goes from `8` to `9`. – William Miller Jan 03 '19 at 06:25
  • @Matt-pow The `for` loop for `_z` converts `_z`, a shape `(10L, )` array into `z` a shape `(100L, )` array populated by 10 instances of each value – William Miller Jan 03 '19 at 06:37
  • 1
    Thanks for the explaination – Matt-pow Jan 03 '19 at 21:45