0

I have a 7x2 matrix, the first column being the quantity and the second being the value:

[[0.5        3.        ]
 [0.5        4.        ]
 [1.         4.        ]
 [0.5        8.        ]
 [0.5        9.        ]
 [0.5        8.        ]
 [0.5        5.99638637]]

For example, the first row says there's 0.5 of value 3. How should I make a histogram where the x-axis depicts the values and the y-axis depicts the quantity, taking into account that values may appear in multiple rows?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Cubostar
  • 11
  • 2

1 Answers1

0

So my suggestion would be to create a numpy array, then iterate through the rows, adding the number of occurrences to a key in a dictionary, initializing the keys in the dictionary as 0.

import collections
import numpy as np
import matplotlib.pyplot as plt

myArray = np.array([[0.5,        3.        ],
                    [0.5,        4.        ],
                    [1.,         4.        ],
                    [0.5,        8.        ],
                    [0.5,        9.        ],
                    [0.5,        8.        ],
                    [0.5,        5.99638637]])

myDict = collections.defaultdict(lambda:0)

for i in range(myArray.shape[0]):
    myDict[myArray[i,1]] += myArray[i,0]

plt.bar(myDict.keys(), myDict.values())
plt.show()
blackdrumb
  • 310
  • 1
  • 8