3

This might sound trivial, but I am unable find a solution in PYTHON. No problem in ROOT or MATLAB.

So, I have a 3x3 array, and I would like each element in the array to represent the height (frequency) of a bin. I should have a histogram with 9 bins. Here's an example of what I have been trying.

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

I've tried histogram2D, I just can't find any to get this to work with PYTHON. Thanks in advance for any help on this.

user1175720
  • 175
  • 1
  • 1
  • 6

1 Answers1

2

If im not mistaken shouldnt this just be:

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

You can also do a 3D histogram:

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

3D Bar histogram:

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

The above should work, I dont have my laptop with me at the moment. More example can be found here. A great example for 3D bars can be found here.

Community
  • 1
  • 1
Daniel
  • 19,179
  • 7
  • 60
  • 74
  • Thank you, that is exactly what I needed. Any chance you know how I can display this as a normal 3D histogram with bars. Thanks again. – user1175720 Jul 11 '13 at 15:11