0

I would like to get the 2d numpy array from the 2dhist function. After obtaining the counts, I would like to plot the data in the pyplot.imshow() function to further add some information. The integers that are allowed to appear in my list are between 0 and 11 (therefore 12 bins).

However, I get a strange matrix.

data1 = [2, 3, 3, 10, 3, 2, 10, 2, 2, 2, 2, 2, 10, 2, 10, 10, 9, 2, 9, 10, 
9, 9, 9, 3, 10, 3, 2, 10, 1]
data2 = [5, 6, 7, 7, 7, 7, 6, 4, 6, 4, 4, 8, 5, 5, 5, 6, 8, 6, 5, 4, 5, 6, 
4, 4, 6, 4, 5, 4, 5]
n_bins = 12
fig, ax = plt.subplots()
counts, xedge, yedge, image = ax.hist2d(data1, data2, bins=n_bins)

result: 
array([[0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
       [3., 0., 0., 3., 0., 0., 2., 0., 0., 1., 0., 1.],
       [2., 0., 0., 0., 0., 0., 1., 0., 0., 2., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [1., 0., 0., 2., 0., 0., 1., 0., 0., 0., 0., 1.],
       [2., 0., 0., 2., 0., 0., 3., 0., 0., 1., 0., 0.]])

transposed:
 array([[0., 3., 2., 0., 0., 0., 0., 0., 0., 0., 1., 2.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [1., 3., 0., 0., 0., 0., 0., 0., 0., 0., 2., 2.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 2., 1., 0., 0., 0., 0., 0., 0., 0., 1., 3.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 2., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]])

If we look at the data lists closely, we see that for example the integer 0 never appears. How can therefore be an entry in the first column?

I would very much appreciate your insides. Thank you.

user3554329
  • 111
  • 2
  • 11
  • Your list have numbers starting from 1 and going up to 10. So 10 unique numbers. Why do you have 12 bins then? There is neither any 0, nor any 11 in either of your lists – Sheldore Jan 17 '19 at 16:16
  • 1
    If you print `xedge` and `yedge` you will see the actual bin edges which start from 1 and go up to 10 : `[ 1. 1.75 2.5 3.25 4. 4.75 5.5 6.25 7. 7.75 8.5 9.25 10. ]`. So the 0 which you see in the counts columns is not the number 0 but the number of values falling in the first bin 1 to 1.75 – Sheldore Jan 17 '19 at 16:21
  • Thank you for your help! Even though my current array only contained numbers from 1 to 10, numbers from 0 to 11 would have been possible. The clue with the xedge and yedge helped a lot. I set now bins=[0,1,2,3,4,5,6,7,8,9,10,11,12] to get the desired shape. – user3554329 Jan 18 '19 at 07:19

0 Answers0