演示直方图函数的不同histtype设置

  • 具有颜色填充的步进曲线的直方图。
  • 具有自定义和不相等的箱宽度的直方图。

选择不同的存储量和大小会显著影响直方图的形状。Astropy文档有很多关于如何选择这些参数的部分: http://docs.astropy.org/en/stable/visualization/histogram.html

不同histtype设置示例

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. np.random.seed(19680801)
  4. mu = 200
  5. sigma = 25
  6. x = np.random.normal(mu, sigma, size=100)
  7. fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(8, 4))
  8. ax0.hist(x, 20, density=True, histtype='stepfilled', facecolor='g', alpha=0.75)
  9. ax0.set_title('stepfilled')
  10. # Create a histogram by providing the bin edges (unequally spaced).
  11. bins = [100, 150, 180, 195, 205, 220, 250, 300]
  12. ax1.hist(x, bins, density=True, histtype='bar', rwidth=0.8)
  13. ax1.set_title('unequal bins')
  14. fig.tight_layout()
  15. plt.show()

下载这个示例