【问题标题】:matplotlib - plotting histogram with unique binsmatplotlib - 用独特的 bin 绘制直方图
【发布时间】:2017-07-24 09:15:03
【问题描述】:

我正在尝试绘制直方图,但 x 刻度似乎不正确。 该图旨在获取频率计数(1 到 13)的直方图和 10000 中的总行数。

d1 = []
for i in np.arange(1, 10000):
tmp = np.random.randint(1, 13)
d1.append(tmp)
d2 = pd.DataFrame(d1)
d2.hist(width = 0.5)
plt.xticks(np.arange(1, 14, 1))

我正在尝试绘制值的频率计数而不是范围。

【问题讨论】:

    标签: matplotlib histogram


    【解决方案1】:

    您需要设置直方图应使用的 bin 边缘。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    d1 = np.random.randint(1, 13, size=1000)
    d2 = pd.DataFrame(d1)
    bins = np.arange(0,13)+0.5
    d2.hist(bins=bins, ec ="k")
    plt.xticks(np.arange(1, 13))
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 2015-10-25
      • 2021-05-10
      • 1970-01-01
      • 2012-01-12
      相关资源
      最近更新 更多