蒙特卡洛模拟色图创建:如何绘制厚度角度对应光子计数的彩色直方图
实现方案
你需要的是二维直方图(热力图),x/y轴分别对应厚度、角度参数,色块颜色映射到对应参数组合下的光子计数,以下是Python生态下的常用实现方式:
前置准备
首先导入依赖库,把你的数据转换为numpy数组方便处理:
import numpy as np import matplotlib.pyplot as plt # 如果你的store_array是Python原生列表,先转为numpy数组 store_array = np.array(store_array) # 拆分三个维度的数值 thickness = store_array[:, 0] angle = store_array[:, 1] photons_count = store_array[:, 2]
场景1:参数为规整网格(所有厚度+角度组合都有对应模拟结果)
如果你的模拟是按固定间隔的厚度、角度网格点逐一跑的,直接重构二维矩阵绘制即可:
# 提取去重排序后的厚度、角度值 unique_thick = np.sort(np.unique(thickness)) unique_angle = np.sort(np.unique(angle)) # 把光子计数重构成和网格匹配的二维矩阵 photon_matrix = photons_count.reshape(len(unique_angle), len(unique_thick)) # 绘图 plt.figure(figsize=(8, 6)) # cmap参数可替换配色,比如plasma、RdYlBu_r等 pcm = plt.pcolormesh(unique_thick, unique_angle, photon_matrix, cmap='viridis') plt.xlabel('箔片厚度') plt.ylabel('入射角度') plt.title('不同参数下的荧光总光子计数') # 添加色条标注颜色对应数值 plt.colorbar(pcm, label='光子计数') plt.show()
场景2:参数为非规整采样(存在缺失组合、重复采样)
如果你的模拟参数是随机采样、或者存在重复测试的情况,用hist2d自动分箱统计即可:
plt.figure(figsize=(8, 6)) # bins参数可指定分箱数量,比如bins=[30,30]就是x/y轴各分30个箱 # weights参数指定每个样本的权重为对应光子计数,统计的是分箱内总光子数 hist, x_edge, y_edge, im = plt.hist2d( thickness, angle, bins=20, weights=photons_count, cmap='viridis' ) plt.xlabel('箔片厚度') plt.ylabel('入射角度') plt.title('不同参数下的荧光总光子计数') plt.colorbar(im, label='分箱内总光子计数') plt.show()
可选调整项
- 要统计分箱内的平均光子计数,可在hist2d中添加参数
density=True - 需要突出高低值差异,可替换
cmap参数为其他配色方案,比如cmap='plasma'为暖色调渐变 - 要让每个参数取值对应一个分箱,可设置
bins=[len(np.unique(thickness)), len(np.unique(angle))]
内容的提问来源于stack exchange,提问作者ruairi shannon
相关产品推荐
相关产品推荐

