How To Plot Keys And Values From Dictionary In Histogram
I need to plot a histogram with the following dictionary x = {5:289, 8:341, 1:1565, 4:655, 2:1337, 9:226, 7:399, 3:967, 6:405} I need first keys be ordered from 1 to 9. Then the v
Solution 1:
From your comment, it seems that a bar chart would be a better way to display the data.
The probability can be found by dividing the values of the dictionary by the sum of the values:
import matplotlib.pyplot as plt
import numpy as np
x = {5:289, 8:341, 1:1565, 4:655, 2:1337, 9:226, 7:399, 3:967, 6:405}
keys = x.keys()
vals = x.values()
plt.bar(keys, np.divide(list(vals), sum(vals)), label="Real distribution")
plt.ylim(0,1)
plt.ylabel ('Percentage')
plt.xlabel ('Significant number')
plt.xticks(list(keys))
plt.legend (bbox_to_anchor=(1, 1), loc="upper right", borderaxespad=0.)
plt.show()
Solution 2:
Sort your data before plotting:
import matplotlib.pyplot as plt
import numpy as np
x = {5:289, 8:341, 1:1565, 4:655, 2:1337, 9:226, 7:399, 3:967, 6:405}
new_x = sorted(x.items(), key=lambda x:x[0])
plt.hist([i[-1] for i in new_x], normed=True, bins=len(new_x), color='g', label = "Real distribution")
plt.show()
Solution 3:
I'm sorry in advance for how terribly un-pythonic and weird my code is. I'm not too strong with mathplot or with numpy.
If you used the_keys = list(set(dict.keys()))
to get a set of the keys (ordered, because it was a set. Like I said in comments, I'm doing some pretty ugly hacking here.) you can then do the_values = [x[i] for i in the_keys]
to get a list representation of the dictionary ordered by keys. Then plot it with
plt.hist(the_keys, the_values, color='g', label = "Real distribution")
plt.show()
Post a Comment for "How To Plot Keys And Values From Dictionary In Histogram"