Scatter plot with different colours

Hopefully this is possible to be done.
Say I have an 10x2 array called "matrix1" and another 10x1 array called "matrix2".
"matrix2" is only made up of 1's and 2's
Using scatterplot I can plot matrix1(:,1) vs matrix1(:,2) easily like so,
scatter(matrix1(:,1),matrix1(:,2))
BUT what I want is to use "matrix2" to colour code the plots. For example if a row in "matrix2" shows a "1" the corresponding row number in "matrix1" is blue on the scatter plot and if "matrix2" shows a "2" the corresponding row number in "matrix1" is red on the scatter plot.
Any ideas would be much appreciated. I have looked at gscatter and groupings but getting a bit lost!
Craig

 Accepted Answer

One input arg of scatter() is a list of colors for the various data points. So just make up a colorlist and pass it in, something like
myColors = zeros(size(matrix1, 1), 3); % List of rgb colors for every data point.
rowsToSetBlue = matrix2 == 1;
rowsToSetRed = matrix2 == 2;
myColors(rowsToSetBlue, :) = [0,0,1];
myColors(rowsToSetRed, :) = [1,0,0];
I haven't test that - it's just off the top of my head. If the last two lines don't work then you might have to use repmat() to make them exactly length(rowsToSetBlue) rows tall.

4 Comments

Could you explain your thinking here? I am not sure I fully understand how this achieves what I want.
See if this explains it any better:
% Create some sample data.
matrix1 = rand(20,2);
% Set up matrix2 to be either 1 or 2 for blue or red
matrix2 = randi(2, size(matrix1, 1), 1)
% You can make up whatever colors you want. The first row of matrix2
% should be the color you want the first row of matrix1 to be, and so on.
myColors = zeros(size(matrix1, 1), 3); % List of rgb colors for every data point.
% Look at matrix2 and determine what color each row should be.
rowsToSetBlue = matrix2 == 1
rowsToSetRed = matrix2 == 2
% Set colormap to blue for the blue rows.
myColors(rowsToSetBlue, 1) = 0;
myColors(rowsToSetBlue, 2) = 0;
myColors(rowsToSetBlue, 3) = 1;
% Set colormap to red for the red rows.
myColors(rowsToSetRed, 1) = 1;
myColors(rowsToSetRed, 2) = 0;
myColors(rowsToSetRed, 3) = 0
% Plot the data with data points in the specified colors.
scatter(matrix1(:, 1), matrix1(:, 2), ...
60, myColors);
grid on;
Ah perfect. That makes sense - thanks alot!
Follow up question here - how do I recognise this in a legend? I have tried to add a legend to this plot and it only picks up the last defined colour (in this case red).
Help appreciated.

Sign in to comment.

More Answers (3)

pointsize = 12;
colormat = matrix1(:,2);
scatter(matrix1(:,1), matrix1(:,2), pointsize, colormat);

2 Comments

I don't understand how this works - it doesn't incorporate "matrix2" at all.

Sign in to comment.

Using GSCATTER is not that difficult:
matrix1 = rand(10,2) ; % [X,Y] data
matrix2 = rand(10,1)>0.5 ; % Grouping (0 or 1)
ph = gscatter(matrix1(:,1),matrix1(:,2), matrix2) % grouped scatter plot
% make it a little prettier
set(ph(1),'color','b','marker','s','markersize',20,'markerfacecolor','y','linewidth',2)

2 Comments

how to put axis limit in gscatter? xlim,ylim doesnt work when i tried
What did you try? When I used Jos's code and added this at the end
xlim([-2,2]);
ylim([-5,10]);
it worked. What did you do differently?

Sign in to comment.

KONSTANTINOS
KONSTANTINOS on 22 Jun 2023
Hi guys I also face the same problem and I would appreciate some help.
I have an 60*8 table where column 1 is "Name" and column 2 is "Run"
Now I also want to do a scatter plot with different shape for the name and different color for the run for the legend but it gets more complicated.
As you see they all are repeated twice and ideally i want to plot for X the first value and for Y the second value (I'm talking rows sorry if i cause any missunderstanding) for example. Francois (lets say shape 'o' ) WeightL (lets say colour 'green') and x would be asymmetry1 row1 and y would be asymmetry row2 and so on till end.
Any ideas?

Asked:

on 25 Jan 2014

Answered:

on 22 Jun 2023

Community Treasure Hunt

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

Start Hunting!