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

  • 带颜色填充的步进曲线直方图。

  • 不带填充的步进曲线直方图。

  • 自定义且不等宽的直方图。

  • 带有堆叠条形的两组直方图。

选择不同的 bin 计数和大小会显著影响直方图的形状。Astropy 文档中有一个关于如何选择这些参数的精彩章节:http://docs.astropy.org/en/stable/visualization/histogram.html

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

mu_x = 200
sigma_x = 25
x = np.random.normal(mu_x, sigma_x, size=100)

mu_w = 200
sigma_w = 10
w = np.random.normal(mu_w, sigma_w, size=100)

fig, axs = plt.subplots(nrows=2, ncols=2)

axs[0, 0].hist(x, 20, density=True, histtype='stepfilled', facecolor='g',
               alpha=0.75)
axs[0, 0].set_title('stepfilled')

axs[0, 1].hist(x, 20, density=True, histtype='step', facecolor='g',
               alpha=0.75)
axs[0, 1].set_title('step')

axs[1, 0].hist(x, density=True, histtype='barstacked', rwidth=0.8)
axs[1, 0].hist(w, density=True, histtype='barstacked', rwidth=0.8)
axs[1, 0].set_title('barstacked')

# Create a histogram by providing the bin edges (unequally spaced).
bins = [100, 150, 180, 195, 205, 220, 250, 300]
axs[1, 1].hist(x, bins, density=True, histtype='bar', rwidth=0.8)
axs[1, 1].set_title('bar, unequal bins')

fig.tight_layout()
plt.show()
stepfilled, step, barstacked, bar, unequal bins

标签:绘图类型:直方图 领域:统计学 用途:参考

参考

本示例展示了以下函数、方法、类和模块的使用

脚本总运行时间: (0 分钟 1.068 秒)

由 Sphinx-Gallery 生成的画廊