Scatter with colour-coded markers (matrix)

55 views (last 30 days)
Elli on 5 Jun 2022
Commented: Adam Danz on 5 Jun 2022
I want to do a scatter with several points x [1303 x 382] and y [1303 x 382]. Each of the points has a different velocity, saved in the matrix velo [1303 x 382].
When I try to do it with scatter(x,y,10,velo), there is an error:
"Color must be one RGB triplet, an m-by-3 matrix of RGB triplets with one color per scatter point, or an m-by-1 vector with one value per scatter point."
How do I do a plot with the points with colour-coded markers based on the velocity?
Thanks for your help!

Answers (3)

Lateef Adewale Kareem
Lateef Adewale Kareem on 5 Jun 2022
Edited: Lateef Adewale Kareem on 5 Jun 2022
minv = min(min(velo));
maxv = max(max(velo));
c = (velo - minv)./(maxv - minv);
Adam Danz
Adam Danz on 5 Jun 2022
the max(max()), min(min()) caught my eye.
@Lateef Adewale Kareem, since the user wants to represent the velocity using color, and not the normalized velocity, the correct solution is to use the raw velocity values in a vector. Add a colorbar and you'll see the difference. But your converstion to vectors is spot-on.

Sign in to comment.

Walter Roberson
Walter Roberson on 5 Jun 2022
scatter(x(:), y(:), 10, velo(:))
You might potentially also want to add a caxis() call, if you want to map velocities to consistent colors.

Image Analyst
Image Analyst on 5 Jun 2022
Edited: Image Analyst on 5 Jun 2022
Try this:
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
% clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Create sample data because the original poster forgot to.
rows = 3; % or 1303
columns = 382;
markerSize = 15;
x = rand(rows, columns);
y = rand(rows, columns);
numElements = numel(x)
% Make velocity in the range 0-15.
velo = 15 * rand(rows, columns);
% Create a colormap based on velo.
% First sort velo
[sortedVelo, sortOrder] = sort(velo(:), 'ascend');
numDistinctColors = numel(velo)
% Create a generic colormap.
cmap = jet(numDistinctColors);
% Now each row in cmap has c color corresponding to the velo value.
% Plot a scatterplot
scatter(x(:),y(:), markerSize, cmap, 'filled')
% Show the colorbar as a guide to what color is what velocity.
minVelocity = 0
maxVelocity = max(velo(:))
caxis([minVelocity, maxVelocity])
title('Color Coded Velocity', 'FontSize',fontSize)
grid on
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Community Treasure Hunt

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

Start Hunting!