Reputation: 3
I'd like to create a heat map to analyze the porosity of some specimens that I have 3D-printed. the X-Y coordinates are fixed since they are the positions in which the specimens are printed on the platform.
Heatmap:
Tbl = readtable('Data/heatmap/above.csv');
X = Tbl(:,1);
Y = Tbl(:,2);
porosity = Tbl(:,3);
hmap_above = heatmap(Tbl, 'X', 'Y', 'ColorVariable', 'porosity');
The first question is: how can I sort the Y-axis of the plot? since it goes from the lower value (top) to the higher value (bottom) and I need it the other way around.
The second question is: I only have around 22 data points and most of the chart is without color, so I'd like to get a smoother heatmap without the black parts.
The data set is quite simple and is shown below:
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 |
Upvotes: 0
Views: 1089
Reputation: 11812
If you want to assign color to the "black parts" you will have to interpolate the porosity over a finer grid than you currently have.
The best tool for 2D interpolation over a uniformly sampled grid is griddata
First you have to define the X-Y grid you want to interpolate over, and choose a suitable mesh density.
% 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) ;
Now my version of matlab does not have the heatmap
function, so I'll just use pcolor
for display.
% now display
hmap_above = pcolor(xq,yq,InterpolatedPorosity);
hmap_above.EdgeColor = [.5 .5 .5] ; % cosmetic adjustment
colorbar
colormap jet
title(['Gridres = ' num2str(gridres)])
And here are the results with different grid resolutions (the value of the gridres
variable at the beginning):
Now you could also ask MATLAB to further graphically smooth the domain by calling:
shading interp
Which in the 2 cases above would yield:
Notes: As you can see on the gridres=100, you original data are so scattered that at some point interpolating on a denser grid is not going to produce any meaningful improvment. No need to go overkill on your mesh density if you do not have enough data to start with.
Also, the pcolor
function uses the matrix input in the opposite way than heatmap
. If you use heatmap
, you have to flip the Y matrix upside down as shown in the code. But if you end up using pcolor
, then you don't need to flip the Y matrix.
The fact that I did it in the code (to show you how to do) made the result display in the wrong orientation for a display with pcolor
. Simply comment the yq = flipud(yq) ;
statement if you stick with pcolor
.
Additionally, if you want to be able to follow the isolevels generated by the interpolation, you can use contour
to add a layer of information:
Right after the code above, the lines:
hold on
contour(xq,yq,InterpolatedPorosity,20,'LineColor','k')
will yield:
Upvotes: 1