Marker on contourf plot
23 views (last 30 days)
Show older comments
Commented: Voss on 28 Dec 2022
I have plotted a contour plot using contourf using Z2. I would like to add markers at certain values in Z2, these values are in LS1 and LS2. But instead of plotting the marker directly on top of the contour plot, I get something like the image:
Does anyone have any idea on the issue? Thank you very much!
When calling contourf with syntax contourf(Z) (i.e., no X and Y), then the x- and y-coordinates of the contour plot are taken to be the column and row indices, respectively, of the matrix Z. That is to say, x is 1:size(Z,2) and y is 1:size(Z,1). (Your Z2 is of size 23-by-23, so your contour goes from x = 1 to x = 23 and y = 1 to y = 23.)
The matrices LS1 and LS2 contain mostly NaNs, and the non-NaN elements are identical to the elements in the corresponding positions in Z2. I figure you want to plot the points where LS1 and LS2 are non-NaN with their respective markers on top of the contour. To do that, you can use find with two outputs (rows and columns), as that will give you the locations consistent with your contour plot.
[r1,c1] = find(~isnan(LS1));
[r2,c2] = find(~isnan(LS2));
More Answers (1)
Akira Agata on 28 Dec 2022
Edited: Akira Agata on 28 Dec 2022
Please use (x,y) coordinate for plotting markers.
The following is an example:
% Find (x,y) coordinate of values
[row1, col1] = find(~isnan(LS1));
[row2, col2] = find(~isnan(LS2));
% Visualize the result
contourf(Z2, 20, "EdgeColor", "none");
scatter(col1, row1, "b+")
scatter(col2, row2, "rx")
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!