六边形空间热图

代码截图恢复与适配 · 来源与修改说明 · 下载完整代码

   1 """六边形空间热图
   2 Restored/adapted from supplied screenshots [104, 105].
   3 Source: 小明的代码美学 (as shown in supplied screenshots).
   4 This is a runnable restoration, not a byte-for-byte original source file.
   5 See SOURCE.md for missing inputs and documented corrections.
   6 """
   7 import numpy as np; from _utils.palette_maps import palette_cmap, palette_stops, contrast_text
   8 import pandas as pd
   9 import matplotlib.pyplot as plt
  10 from matplotlib.colors import LinearSegmentedColormap, Normalize, to_hex
  11 from matplotlib.cm import ScalarMappable
  12 from pathlib import Path
  13 # Missing .mplstyle files are replaced only by explicit, portable display defaults.
  14 plt.rcParams.update({'font.sans-serif':['Microsoft YaHei','DejaVu Sans'],
  15  'axes.unicode_minus':False,'figure.facecolor':'white','axes.facecolor':'white',
  16  'axes.spines.top':False,'axes.spines.right':False,'font.size':9})
  17 def create_gaussian_points(center,amplitude,sigma_x,sigma_y,num_points):
  18     x=np.random.normal(center[0],sigma_x,num_points)
  19     y=np.random.normal(center[1],sigma_y,num_points)
  20     z=amplitude*np.exp(-((x-center[0])**2/(2*sigma_x**2)+(y-center[1])**2/(2*sigma_y**2)))
  21     return x,y,z
  22 np.random.seed(2);num_points=5000
  23 x1,y1,z1=create_gaussian_points((10,7),-50,28,25,num_points)
  24 x2,y2,z2=create_gaussian_points((25,12),50,27,26,num_points)
  25 x=np.concatenate([x1,x2]);y=np.concatenate([y1,y2])
  26 z=np.concatenate([z1,z2])+np.random.uniform(-25,25,2*num_points)
  27 cmap=LinearSegmentedColormap.from_list('custom',palette_stops('diverging',count=5),N=256)
  28 fig,ax=plt.subplots(figsize=(10,6),dpi=150)
  29 hb=ax.hexbin(x,y,C=z,gridsize=50,cmap=cmap,reduce_C_function=np.mean,edgecolors='white',linewidths=.5)
  30 cb=fig.colorbar(hb,ax=ax,orientation='vertical',pad=.01,fraction=.05)
  31 cb.set_label('格内测量值均值')
  32 ax.set(title='六边形空间热图',xlim=(-60,90),ylim=(-40,50),xlabel='X 轴',ylabel='Y 轴')
  33 plt.tight_layout();plt.show()
  34