Hexbin 演示

使用Matplotlib绘制hexbins。

Hexbin是一种轴方法或pyplot函数,它基本上是具有六边形单元的二维直方图的pcolor。 它可以比散点图更具信息性。 在下面的第一个图中,尝试用’scatter’代替’hexbin’。

Hexbin演示

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. # Fixing random state for reproducibility
  4. np.random.seed(19680801)
  5. n = 100000
  6. x = np.random.standard_normal(n)
  7. y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
  8. xmin = x.min()
  9. xmax = x.max()
  10. ymin = y.min()
  11. ymax = y.max()
  12. fig, axs = plt.subplots(ncols=2, sharey=True, figsize=(7, 4))
  13. fig.subplots_adjust(hspace=0.5, left=0.07, right=0.93)
  14. ax = axs[0]
  15. hb = ax.hexbin(x, y, gridsize=50, cmap='inferno')
  16. ax.axis([xmin, xmax, ymin, ymax])
  17. ax.set_title("Hexagon binning")
  18. cb = fig.colorbar(hb, ax=ax)
  19. cb.set_label('counts')
  20. ax = axs[1]
  21. hb = ax.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
  22. ax.axis([xmin, xmax, ymin, ymax])
  23. ax.set_title("With a log color scale")
  24. cb = fig.colorbar(hb, ax=ax)
  25. cb.set_label('log10(N)')
  26. plt.show()

下载这个示例