Whiteout regions on Contour Plot

I don't want to display certain regions of the contour plot. How can I whiteout these regions on the contour plot? The interpolated data fills the entire space while the real data has blank regions that physically don't contain anything. These blank regions are the regions that I don't want to display on the contour plot. I have attached the two plots.

I have the scatter plot data as x,y,z arrays and the interpolated data for the contour plot as x,y,z matrices.

Any help/suggestion is welcomed and appreciated. Thank you.

 Accepted Answer

Walter Roberson
Walter Roberson on 14 Dec 2017
My recommendation would be to set the interpolated data to nan in those regions before doing the contour plot.
However, since you have scattered data, you have the challenge of determining which points are "near enough" to explicit scattered data that they should validly be interpolated, and which points are instead to be interpreted as being too far from the scattered data and so should be interpreted as being outside the map.
You could consider a pdist2() approach but it appears you have a fair bit of data, so that would not be computationally feasible. You might be able to take advantage of kd-tree approaches; https://www.mathworks.com/help/stats/classification-using-nearest-neighbors.html

6 Comments

@ Walter,
Yes, I have about 20 million data points in the scatter plot. I will try the pdist2().
My other question relating to your suggestion is- How can I set the interpolated data to NaN after I decide which data points to erase using pdist2()? My interpolated data are in x,y,z matrices, while my scatter plot data are x,y,z arrays. How can I compare array values with matrix values? I vaguely remember arrays can be reshaped into matrices, but my x,y,z arrays contains 20 million data points each...What's your suggestion on comparing the array values with the interpolated values in matrix form?
Thank you!
To be more specific, the matrices for the contour plot are 76x96 each for x,y and z. The arrays of the scatter data are actually 38000000 x 1 each for x, y and z.
Are you doing something similar to
qxvec = linspace(min(x), max(x), 76);
qyvec = linspace(min(y), max(y), 96);
[qx, qy] = ndgrid(qxvec, qyvec);
qz = griddedInterpolant(x, y, z, qx, qy);
In such a case then by default you would be using bilinear interpolation, so the value at each gridpoint qx(I,J), qy(I,J) would be determined by the four points in the x, y vectors nearest to the query points, and no other data points would be used.
Or are you doing something like a density plot, or something like a maximum intensity projection plot? Or something like creating a grid and determining where each point fits on it, and averaging all of the points in any one grid location?
If you did happen to be using an approach involving gridding the points and then considering the properties of the subset of points within each grid location, then that would make it relatively easy to determine which grid locations were empty and to assign nan to them. The trade off in such a case is that there could well be an adjacent point "just outside" the grid location whose influence would not be considered at all. But that might be acceptable for your purposes.
My code for gridding the data is:
[xi,yi] = meshgrid(300:20:2200, 300:20:1800);
zi = griddata(xn,yn,zn,xi,yi);
The meshgrid ranges were chosen according to the scatter plot.
cutoff_distance = 6.28; %set as appropriate
[xi,yi] = meshgrid(300:20:2200, 300:20:1800);
zi = griddata(xn, yn, zn, xi, yi);
DT = delaunayTriangulation(xn, yn);
[vi, d] = nearestNeighbor(DT, xi(:), yi(:));
mask = d > cutoff_distance;
zi(mask) = nan;
Thank you so much! It works now!

Sign in to comment.

More Answers (1)

Muhammad Usman
Muhammad Usman on 6 Nov 2019
I have got the same problem but mine was a little bit simple. I use the built-in MATLAB function "scatterInterpolant" to plot a contour. The plot is shown below.
Sigma_xx.jpg
It's s stress contour and the ellipse is an empty space and there are no nodes inside the ellipse. I want the ellipse to be whiteout. Somehow I managed to set the values inside the ellipse to be zero (because I can't delete them) and I got the following result
Sigma1_xx.jpg I want the ellipse to be whiteout. Any sort of help is highly appreciated. Thanks in advance.
P.S. I use the fill command to fill the ellipse with white color but that's not a proper way to do it.

8 Comments

You can set the values to nan in the ellipse instead of zero. If you specifically need white in that area, then the easiest way might be to use nan there but put a white patch behind that will show through where there are nan.
Thank you very much sir. NaN worked.
How to put a white patch behind?
If anything is 3d then patch() with z coordinate lower than any used value. Otherwise, if you are using 2d only, fill() and use uistack to put it behind the other objects.
Sir...may I know what is the method that you are using to find the data inside the eclipse and set the value into NaN at your second picture?
I have the similar problem that only have the outer boundary for the ellipse same as the picture one..
Maybe you can use the inpolygon function
[in,on] = inpolygon(x_list,y_list,ellipse_x,ellipse_y);
for i = 1:length(x_list)
if(in(i))
ellipse_x(i)=NaN;
ellipse_y(i)=NaN;
end
end
That suggests to me that you could use the shorter
[in,on] = inpolygon(x_list,y_list,ellipse_x,ellipse_y);
ellipse_x(in) = NaN;
ellipse_y(in) = NaN;

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!