【发布时间】:2021-01-22 01:50:26
【问题描述】:
【问题讨论】:
标签: python matlab plot 2d heatmap
【问题讨论】:
标签: python matlab plot 2d heatmap
下面是一种方法,它使用meshgrid() 函数为Temperature 创建一个域,以便在/ 上绘制。要绘制 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 中,如果你有一个矩阵形式的数据,比如你的表格,你可以使用 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()
【讨论】: