Marker on contourf plot
27 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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);
更多回答(1 个)
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;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!