大家好,我是林译川。今天我们来聊聊matplotlib.pyplot中的hist函数,这个函数可是绘制直方图的神器哦!
什么是直方图?
首先,得区分一下直方图和条形图。条形图就像一根根柱子,柱子的高度代表频数,宽度固定;而直方图则像一块块砖头,砖头的高度代表频数,宽度代表组距。简单来说,直方图更适合展示连续型数据的分布情况。
hist函数的基本用法
import numpy as np
import matplotlib.pyplot as plt
# 概率分布直方图
# 高斯分布
mean = 0
sigma = 1
x = mean + sigma * np.random.randn(10000)
fig, (ax0, ax1) = plt.subplots(nrows=2, figsize=(9, 6))
ax0.hist(x, 40, normed=1, histtype='bar', facecolor='yellowgreen', alpha=0.75)
ax0.set_title('pdf')
ax1.hist(x, 20, normed=1, histtype='bar', facecolor='pink', alpha=0.75, cumulative=True, rwidth=0.8)
ax1.set_title(
