【问题标题】:How to create a smoother heatmap如何创建更平滑的热图
【发布时间】:2021-04-06 14:26:59
【问题描述】:

我想创建一个热图来分析我 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');
  • 第一个问题是:如何对绘图的 Y 轴进行排序?因为它从较低的值(顶部)到较高的值(底部),而我需要它。

  • 第二个问题是:我只有大约 22 个数据点,而且大部分图表没有颜色,所以我想得到一个没有黑色部分的更平滑的热图。

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

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

【问题讨论】:

  • 听起来像旋转和插值可能会有所帮助。如果您有关于您正在使用的表/数组的更多详细信息,那么我们可能会提供更具体的实现细节。
  • 谢谢,我刚刚添加了使用的表,很简单

标签: matlab heatmap


【解决方案1】:

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

在均匀采样的网格上进行 2D 插值的最佳工具是 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) ; 语句即可。


另外,如果你希望能够遵循插值生成的等值线,可以使用contour添加一层信息: 在上面的代码之后,以下几行:

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

将产生:

【讨论】:

  • 我决定将功能 Heatmap 更改为 pcolor。这非常有用。谢谢
猜你喜欢
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
相关资源
最近更新 更多