【发布时间】:2018-05-22 04:54:25
【问题描述】:
我需要用以下字典绘制直方图
x = {5:289, 8:341, 1:1565, 4:655, 2:1337, 9:226, 7:399, 3:967, 6:405}
我需要从 1 到 9 对第一个键进行排序。然后这些值将绘制在直方图中,显示最大概率为 1.0。我已经尝试了以下(加上其他东西)。
import matplotlib.pyplot as plt
import numpy as np
plt.hist(x.keys(), x.values(), color='g', label = "Real distribution")
plt.show()
或者
plt.hist (x, bins = np.arange(9), color = 'g', label = "Real distribution")
plt.show()
或者
fsn_count_ = sorted(fsn_count)
plt.hist (fsn_count_, bins = np.arange(9), color = 'b', label = "Real distribution")
plt.plot ([0] + bf, color = 'g', label = "Benford Model")
plt.xlabel ('Significant number')
plt.ylabel ('Percentage')
plt.xlim (1,9)
plt.ylim (0,1)
plt.legend (bbox_to_anchor = (1, 1), loc="upper right", borderaxespad=0.)
plt.savefig (country_ + '.png')
plt.show ()
plt.clf ()
distribution_sum = sum(bf)
print('The sum of percentage distribution is:', distribution_sum)
【问题讨论】:
-
请注意,字典不是为有序键设计的。
x.keys()将为您提供x的所有密钥,但不一定按照您上次调用它时的顺序。 -
那么,存储这些信息的好方法是什么?我确实需要知道数字五、六... n 出现了多少次,然后绘制它。
-
首先,您使用的是python 3.x还是python 2.x?
-
我使用的是 Python 3.x
-
没关系,Ajax1234 击败了我,并且使用了一种比我要拼凑起来的任何一种更优雅的解决方案。
标签: python dictionary matplotlib plot