Reputation: 145
I have a python directory, the keys of which stores the percentage range: 0_5, 5_10, 10_15,....80_85, 85_90, 90_95, 95_100. The value for each key is the count number in the whole data. I want to use matplotlib to plot a histogram to see its distribution and there will be 20 bins and each bin should be labeled with its percentage range and there will be a little spacing between each bin so that they are seperated.
I've tried this code and it gives me histogram that has 20 bins. But it's not what I need.
commutes = pd.Series(counts)
commutes.plot.hist(grid = False, bins = 20, rwidth = 0.8, color = 'tomato', edgecolor='gray', label = 'Type 1')
Also, I tried this and it shows the error: ValueError: weights should have the same shape as x
pylab.hist(ratio.keys(), weights = ratio.values(), bins=range(20))
This is how I created the directory. The variable counts stores a list of 700 percentage values.
for i in range(0,100,5):
start = i
end = i+5
key = str(start)+"_"+str(end)
number = 0
for count in counts:
if((count >= start) and (count < end)):
number = number + 1
ratio[key] = number
Upvotes: 2
Views: 959
Reputation: 6322
ratio.keys()
and ratio.values()
are of type dict_keys
and dict_values
. I'm guessing that matplotlib is trying to apply np.array()
or np.asarray()
to them, which does not work as intended, i.e. it gets converted to array(dict_keys([...]), dtype=object)
rather than an array of numbers.
A simple fix is to convert the dictionary keys and values to lists first.
pylab.hist(list(ratio.keys()), weights=list(ratio.values()), bins=range(len(ratio)))
Upvotes: 0