Marker on contourf plot

조회 수: 27 (최근 30일)
Eli
Eli 2022년 12월 28일
댓글: Voss 2022년 12월 28일
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!

채택된 답변

Voss
Voss 2022년 12월 28일
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
Eli
Eli 2022년 12월 28일
Thank you for the detailed explanation!
Voss
Voss 2022년 12월 28일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Akira Agata
Akira Agata 2022년 12월 28일
편집: Akira Agata 2022년 12월 28일
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;
  댓글 수: 1
Eli
Eli 2022년 12월 28일
Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by