我有一本名为
的字典样本 = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.103080332 84258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.5131979656 0976771}
这里字典的keys
表示x轴上的点,字典的values
表示该特定x的点的值
即y=f(x)
。
如何使用 0-10
区间内的 bin size=1
创建直方图
最佳答案
您可以使用键
作为分箱位置,使用值
作为直方图的权重,执行以下操作:
import numpy as np
import matplotlib.pyplot as plt
d = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}
a = np.array(d.items())
a = a[np.argsort(a[:,0])]
x, weights = a.T
plt.figure(figsize=(5,4))
plt.hist(x, weights=weights)
plt.show()
给出:
关于python - 字典到直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23669199/