Skip to main content
 首页 » 编程设计

Python/Matplotlib之表示平均值的颜色条

2024年12月31日9bhlsheji

是否可以在颜色条上指示平均值?

我有以下图表,显示了撒哈拉沙漠部分的表面“温度”(辐射),现在很高兴看到箭头或其他指示的颜色条上的平均值。

绘图之间的差异在于进行测量的波段/ channel /波长,并且存在细微差别。尤其是当我要比较每个季节的数据时。

请您参考如下方法:

当您使用 plt.colorbar() 将颜色条添加到图中时,matplotlib 会为颜色条创建一个新轴并返回颜色条对象。绘制颜色条的轴在 x 和 y 中从 0 缩放到 1,并被引用为颜色条对象的 .ax 属性。我们可以使用颜色条中的最小值和最大值来映射应该在轴上绘制平均值的位置。

import numpy as np 
from matplotlib import pyplot as plt 
 
data = np.random.normal(1, 4, 450).reshape(-1, 15) 
plt.imshow(data) 
# capture the colorbar object, rescale mean to the axis 
cb = plt.colorbar() 
mean_loc = (data.mean() - cb.vmin) / (cb.vmax - cb.vmin) 
 
# add a horizontal line to the colorbar axis 
cb.ax.hlines(mean_loc, 0, 1) 
 
plt.show()