如何绘制 3 个变量(深度、时间、参数)的热图

How to plot heatmap of 3 variables (depth, time, parameter)

我有随时间和深度变化的温度数据(见附件示例图片)。不过,我的实际数据集要大得多。我想知道如何绘制热图。我对 Python 或 Matlab 持开放态度。

谢谢。

在 MATLAB 中

下面是一种使用 meshgrid() 函数为要绘制的 Temperature 创建域的方法 on/against。要绘制 2D 热图,我们可以使用 surf() 并使用 view() 函数将仰角设置为俯视图(90 度)。根据您是否希望进行插值,可以包含或删除 shading interp 的使用。要获取时间标签,我们可以将 Time_Vector 转换为字符串数组并使用 arrayfunc()(数组函数)将点 . 替换为冒号 :。最后,我们可以在当前轴上使用 set() 函数,gca 在绘图上显示新格式化的时间标签。 colormap()可以设置为'hot''winter''spring'等多种选项

Time_Vector = (10.00: 0.01: 10.09);
Depth_Vector = (1:3);
Temperature = [15 16 17 18 19 20 20 20 20 20;
               25 30 35 40 45 50 50 50 50 50;
               30 35 40 45 50 55 60 65 70 75];

[Time_Grid,Depth_Grid] = meshgrid(Time_Vector,Depth_Vector);

surf(Time_Grid,Depth_Grid,Temperature);
title("Heatmap");
xlabel("Time"); ylabel("Depth");
colormap(hot);
shading interp
Angle = 0; Elevation = 90;
view(Angle,Elevation);
colorbar;

%Time label adjustments%
Time_Labels = string(Time_Vector);
Time_Labels = arrayfun(@(x) replace(x,".",":"),Time_Labels);
set(gca,'xtick',Time_Vector,'xticklabel',Time_Labels);

运行 使用 MATLAB R2019b

在 python 中,如果您的数据是矩阵形式的,例如您的 table,您可以使用 matplotlib imshow 函数。缺点是刻度标签将是列表中的位置,因此您需要使用 FuncFormatter 选项传递一个函数来转换数据值中的列表位置。一个例子:

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import FixedLocator, AutoLocator, FuncFormatter


#Just creating a sample data
dep = np.array([1,2,3])
time = np.arange(0,101)
dp,tm = np.meshgrid(dep,time)

matrix = np.flip(600*np.exp(-tm*dep/50),axis = 0)
#in this case we will have 3 columns, one for each depth

#ploting:
fig,ax = plt.subplots()

img = ax.imshow(matrix,cmap = 'OrRd',vmin = 0,vmax = 600,aspect = 'auto')

#set color bar
fig.colorbar(img,label = 'Temperature')

#Now we will correct the the x axis tick labels
ax.xaxis.set_major_formatter(FuncFormatter(lambda *x: x[0]+1))
#And fix the positions if you dont want tick labels like 2.5 wich are not in the data
ax.xaxis.set_major_locator(FixedLocator([0,1,2]))

ax.set_xlabel('Deph')

#correct the y axis tick labels
ax.yaxis.set_major_formatter(FuncFormatter(lambda *x: '%i'%(time[-1] - x[0])))
ax.set_ylabel('Time')

plt.show()