文章目录[ Matplotlib version: 3.2.1 ] 七、频次直方图、数据区间划分和分布密度(一)简易频次直方图%matplotlib inlineimport numpy as npimport matplotlib.pyplot as plt plt.style.use('seaborn-white')date = np.random.randn(1000)plt.hist(data) 自定义频次直方图 plt.hist(data, bins=30, density=True, alpha=0.5, histtype='stepfilled', color='steelblue', edgecolor='none') 同坐标轴的多个频次直方图 用频次直方图对不同分布特征的样本进行对比时,将 x1 = np.random.normal(0, 0.8, 1000)x2 = np.random.normal(-2, 1, 1000)x3 = np.random.normal(3, 2, 1000)kwargs = dict(histtype='stepfilled', alpha=0.3, density=True, bins=40)plt.hist(x1, **kwargs)plt.hist(x2, **kwargs)plt.hist(x3, **kwargs) 如果只需要简单计算频次直方图(计算每段区间的样本数),而不像画图显示它们,可以直接用 counts, bin_edges = np.histogram(data, bins=5)counts# array([ 36, 255, 445, 230, 34]) (二)二维频次直方图与数据区间划分如同将一维数组分为区间创建一维频次直方图,也可以将二维数组按照二维区间进行切分,创建二维频次直方图 首先,用一个多元高斯分布(multivariate Gaussian distribution)生成x轴与y轴的样本数据 mean = [0, 0]cov = [[1, 1], [1, 2]]x, y = np.random.multivariate_normal(mean, cov, 10000).T 1. plt.hist2d:二维频次直方图画二维频次直方图最简单的方法就是使用Matplotlib的 plt.hist2d(x, y, bins=30, cmap='Blues')cb = plt.colorbar()cb.set_label('counts in bin')
counts, xedges, yedges = np.histogram2d(x, y, bins=30) 2. plt.hexbin:六边形区间划分二维频次直方图是由与坐标轴正交的方块分割而成的,还有一种常用的方式是用正六边形分割。 Matplotlib提供 plt.hexbin(x, y, gridsize=30, cmap='Blues')cb = plt.colorbar(label='count in bin') 3. 核密度估计还有一种评估多维数据分布密度的常用方法是核密度估计(kernel density estimation, KDE) 简单演示如何用KDE方法“抹掉”空间中离散的数据点,从而拟合出一个平滑的函数。( from scipy.stats import gaussian_kde# 拟合数组维度[Ndim, Nsamples]data = np.vstack([x, y])kde = gaussian_kde(data)# 用一对规则的网格数据进行拟合xgrid = np.linspace(-3.5, 3.5, 40)ygrid = np.linspace(-6, 6, 40)Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)Z = kde.evaluate(np.vstack([Xgrid.ravel(), Ygrid.ravel()]))# 画出结果图plt.imshow(Z.reshape(Xgrid.shape), origin='lower', aspect='auto', extent=[-3.5, 3.5, -6, 6], cmap='Blues')cb = plt.colorbar()cb.set_label('density')
Matplotlib 相关阅读: [Python3] Matplotlib —— (一) 入门基础 总结自《Python数据科学手册》 |
|
来自: LibraryPKU > 《制图》