How can I make a 2D color plot

조회 수: 3 (최근 30일)
felix kok
felix kok 2018년 7월 5일
댓글: Star Strider 2018년 7월 6일
I have an NxM matrix (B) containing values. Every value N,M corresponds to a value at that position in x,y (similarly, I also have the meshgrids for x and y). What I would like to plot is simply a contour in x,y giving in binary state whether or not the value of B is higher than a threshold. I.e. a plot where every value above threshold gives a red dot and every value below threshold gives a green dot. How do I do this?
Thanks in advance!
  댓글 수: 2
jonas
jonas 2018년 7월 5일
Do you want something like this?
felix kok
felix kok 2018년 7월 5일
Yes please!

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

답변 (1개)

Star Strider
Star Strider 2018년 7월 5일
Try this:
x = rand(1, 5); % Create Data
y = rand(1, 7); % Create Data
[X,Y] = ndgrid(x,y);
B = rand(5, 7); % Create Data
Bidx = B >= 0.5; % Threshold Index
B1 = B .* Bidx; % Matrix == Threshold Condition
B1(B1 == 0) = NaN; % Only Plot Points Meeting Criteria
B2 = B .* ~Bidx; % Matrix ~= Threshold Condition
B2(B2 == 0) = NaN; % Only Plot Points Meeting Criteria
figure
scatter3(X(:), Y(:), B1(:), '.r')
hold on
scatter3(X(:), Y(:), B2(:), '.g')
hold off
view(0, 90)
  댓글 수: 2
felix kok
felix kok 2018년 7월 6일
편집: felix kok 2018년 7월 6일
Thanks a lot! However, when I try your code I get the following (see image)
Star Strider
Star Strider 2018년 7월 6일
You did not specify the result you wanted.
Try this:
x = rand(1, 50); % Create Data
y = rand(1, 70); % Create Data
[X,Y] = ndgrid(x,y);
B = rand(numel(x), numel(y)); % Create Data
Bidx = B >= 0.5; % Threshold Index
B1 = B .* Bidx; % Matrix == Threshold Condition
figure
hs = surf(X, Y, B1)
set(hs, 'EdgeColor','none')
view(0, 90)
colormap([1 0 0; 0 1 0])
colorbar
Experiment to get different results.

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

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by