如何创建更平滑的热图

How to create a smoother heatmap

我想创建一个热图来分析我 3D 打印的一些样本的孔隙率。 X-Y 坐标是固定的,因为它们是样品在平台上打印的位置。

热图:

Tbl = readtable('Data/heatmap/above.csv');
X = Tbl(:,1);
Y = Tbl(:,2);
porosity = Tbl(:,3);
hmap_above = heatmap(Tbl, 'X', 'Y', 'ColorVariable', 'porosity');

数据集比较简单,如下图:

X Y porosity
74.4615 118.3773 0.039172163
84.8570 69.4699 0.046314637
95.2526 20.5625 0.041855213
105.6482 -28.3449 0.049796110
116.0438 -77.2522 0.045010692
25.5541 107.9817 0.038562053
35.9497 59.0743 0.041553065
46.3453 10.1669 0.036152061
56.7408 -38.7404 0.060719664
67.1364 -87.6478 0.037756115
-23.3533 97.5861 0.052840845
-12.9577 48.6787 0.045216851
-2.5621 -0.2286 0.033645353
7.8335 -49.1360 0.030670865
18.2290 -98.0434 0.024952472
-72.2607 87.1905 0.036199237
-61.8651 38.2831 0.026725885
-51.4695 -10.6242 0.029212058
-41.0739 -59.5316 0.028572611
-30.6783 -108.4390 0.036796151
-121.1681 76.7949 0.031688096
-110.7725 27.8876 0.034619855
-100.3769 -21.0198 0.039070101
-89.9813 -69.9272 NaN
-79.5857 -118.8346 NaN

如果您想为“黑色部分”指定颜色,则必须在比当前更精细的网格上插入孔隙率。

在均匀采样网格上进行二维插值的最佳工具是griddata

首先您必须定义要插值的 X-Y 网格,并选择合适的网格密度。

% this will be the number of points over each side of the grid
gridres = 100 ;
% create a uniform vector on X, from min to max value, made of "gridres" points
xs = linspace(min(X),max(X),gridres) ;
% create a uniform vector on Y, from min to max value, made of "gridres" points
ys = linspace(min(Y),max(Y),gridres) ;
% generate 2D grid coordinates from xs and ys
[xq,yq]=meshgrid(xs,ys) ;
% now interpolate the pososity over the new grid
InterpolatedPorosity = griddata(X,Y,porosity,xq,yq) ;

% Reverse the Y axis (flip the `yq` matrix upside down)
yq = flipud(yq) ;

现在我的matlab版本没有heatmap功能,所以我就用pcolor显示。

% now display
hmap_above = pcolor(xq,yq,InterpolatedPorosity);
hmap_above.EdgeColor = [.5 .5 .5] ; % cosmetic adjustment
colorbar
colormap jet
title(['Gridres = ' num2str(gridres)])

下面是不同网格分辨率的结果(gridres开头变量的值):

现在您还可以要求 MATLAB 通过调用进一步以图形方式平滑域:

shading interp

在上述两种情况下会产生:


注意:正如您在 gridres=100 上看到的那样,您的原始数据非常分散,以至于在某些时候在更密集的网格上进行插值不会产生任何有意义的改进。如果您没有足够的数据开始,则无需过大地提高网格密度。

此外,pcolor 函数以与 heatmap 相反的方式使用矩阵输入。如果使用 heatmap,则必须如代码所示将 Y 矩阵倒置。但是如果你最终使用pcolor,那么你不需要翻转Y矩阵。

事实上,我在代码中这样做(向您展示如何做)导致结果显示方向错误,无法显示 pcolor。如果您坚持 pcolor.

,只需注释 yq = flipud(yq) ; 语句

另外,如果你希望能够跟随插值生成的isolevels,你可以使用contour来添加一层信息: 在上面的代码之后,行:

hold on
contour(xq,yq,InterpolatedPorosity,20,'LineColor','k')

将产生: