How can I make a 2D color plot

3 views (last 30 days)
felix kok
felix kok on 5 Jul 2018
Commented: Star Strider on 6 Jul 2018
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!

Answers (1)

Star Strider
Star Strider on 5 Jul 2018
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 Comments
felix kok
felix kok on 6 Jul 2018
Edited: felix kok on 6 Jul 2018
Thanks a lot! However, when I try your code I get the following (see image)
Star Strider
Star Strider on 6 Jul 2018
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!