Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D Histogram from a 3x3 array in PYTHON

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.

like image 326
user1175720 Avatar asked Aug 14 '26 19:08

user1175720


1 Answers

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.

like image 77
Daniel Avatar answered Aug 18 '26 06:08

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!