Reputation: 175
I have a dictionary that looks liks this:
dict = {1092267: '0.187', 524292: '-0.350', 524293: '0.029', 524294: '0.216'}
So there is an id, and then a value that's inside a string (the real dictionary contains 10 000 of these id's). I want to make a histogram with on the x axis the absolute values of the values inside those strings from lets say 0-0.1, 0.1-0.2, 0.2-0.3 etc all the way to 0.9-1.0. The y axis should count the amount of occurences of the values within these ranges of 0-0.1 etc. How do I do this???
Upvotes: 2
Views: 3412
Reputation: 2826
You can use numpy.histogram
to create the histogram's bins.
First of all, get the absolute values of all the dictionary's values (since ids are irrelevant).
dict_values = [abs(float(i)) for i in dict.values()]
Then, use numpy.histogram
specifying the range of the values explicitly.
import numpy as np
hist = np.histogram(dict_values, range=(0.0,1.0))
Upvotes: 3
Reputation: 152
Without using numpy.histogram, you can try:
test = {1092267: '0.187', 524292: '-0.350', 524293: '0.029', 524294: '0.216'}
intervals = [(-2, 0.1), (0.1, 0.2), (0.2, 0.3), (0.3, 0.4)]
count = []
for inf, sup in intervals:
count.append(len([x for x in test.values() if inf < float(x) < sup]))
Then, count, which is the histogram, will have [2, 1, 1, 0]. This way you can specify arbitrary ranges to define the bins. If you want to plot it, you can use matplotlib (example here).
1: http://matplotlib.org/1.3.0/examples/pylab_examples/histogram_demo_extended.html
Upvotes: 0