Marker on contourf plot

45 ビュー (過去 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 Center および File ExchangeContour Plots についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by