Marker on contourf plot

26 views (last 30 days)
Eli
Eli on 28 Dec 2022
Commented: Voss on 28 Dec 2022
Dear all,
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:
load('Test.mat');
contourf(Z2,20,'edgecolor','none');
hold on;
plot(LS1,'b+','MarkerSize',5);
hold on;
plot(LS2,'rx','MarkerSize',5);
colorbar;
Does anyone have any idea on the issue? Thank you very much!

Accepted Answer

Voss
Voss on 28 Dec 2022
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.
load Test
contourf(Z2,20,'edgecolor','none');
colormap(jet())
colorbar
hold on
[r1,c1] = find(~isnan(LS1));
[r2,c2] = find(~isnan(LS2));
plot(c1,r1,'b+','MarkerSize',5);
plot(c2,r2,'rx','MarkerSize',5);
  2 Comments
Eli
Eli on 28 Dec 2022
Thank you for the detailed explanation!
Voss
Voss on 28 Dec 2022
You're welcome!

Sign in to comment.

More Answers (1)

Akira Agata
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:
load('Test.mat');
% Find (x,y) coordinate of values
[row1, col1] = find(~isnan(LS1));
[row2, col2] = find(~isnan(LS2));
% Visualize the result
figure
contourf(Z2, 20, "EdgeColor", "none");
hold on
scatter(col1, row1, "b+")
scatter(col2, row2, "rx")
colorbar;

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!