Python绘制六边形分箱图

Python绘制六边形分箱图 hexbin 是一种二维直方图绘图其中的分箱是六边形颜色表示每个分箱内的数据点数量。import matplotlib.pyplot as plt import numpy as np # 固定随机状态以保证可重复性 np.random.seed(19680801) n 100_000 x np.random.standard_normal(n) y 2.0 3.0 * x 4.0 * np.random.standard_normal(n) xlim x.min(), x.max() ylim y.min(), y.max() fig, (ax0, ax1) plt.subplots(ncols2, shareyTrue, figsize(9, 4)) hb ax0.hexbin(x, y, gridsize50, cmapinferno) ax0.set(xlimxlim, ylimylim) ax0.set_title(Hexagon binning) cb fig.colorbar(hb, axax0, labelcounts) hb ax1.hexbin(x, y, gridsize50, binslog, cmapinferno) ax1.set(xlimxlim, ylimylim) ax1.set_title(With a log color scale) cb fig.colorbar(hb, axax1, labelcounts) plt.show()参考文献https://matplotlib.org/stable/gallery/statistics/hexbin_demo.html