Contour plot in hexagon form
6 views (last 30 days)
Show older comments
I have some data for a evenly distributed grid inside a hexagon. I would like to do a contour plot which will look like this:

The data I am trying to plot is uploaded in a MAT file here. Here x and y are the horizontal and vertical values of coordinates respectively.
The plot of the coordinates looks like this:

What I have tried to do is given below,
clearvars;
load('data.mat');
[X,Y]= meshgrid(x, y);
lx=length(X);
Z= zeros(lx,lx); % initializing matrix
nopoints=length(x);
count=1;
for ix=1:lx
for iy=1:lx
if (count <= nopoints)
if(X(ix,iy)==x(count) && Y(ix,iy)==y(count))
Z(ix,iy)=z(count);
count = count+1;
else
Z(ix,iy) = NaN;
end
else
Z(ix,iy)= NaN;
end
end
end
figure;
contour(X,Y,Z);
Unfortunately the plot shows nothing like the first image. With data cursor I can see there are discrete data points but no contour plot. Am I missing something?
Any help regarding this is greatly appreciated. :)
2 Comments
Answers (1)
Kelly Kearney
on 17 Aug 2016
Take a look at the sparsity pattern of the matrix you built to represent your grid:
spy(~isnan(Z))
I don't entirely follow the logic of your code, but it's definitely not gridding the data like you want. The easiest way to do this is with scatteredInterpolant:
f = scatteredInterpolant([x y], z, 'nearest', 'none');
[xg, yg] = meshgrid(unique(x), unique(round(y,2)));
zg = f(xg,yg);
scatter(x,y,[],z);
hold on;
contour(xg,yg,zg);
0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!