# Scatter with colour-coded markers (matrix)

26 次查看（过去 30 天）
Elli2022-6-5

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?

### 回答（3 个）

minv = min(min(velo));
maxv = max(max(velo));
c = (velo - minv)./(maxv - minv);
scatter(x(:),y(:),10,c(:))
##### 3 个评论显示隐藏 2更早的评论
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.

Walter Roberson 2022-6-5
scatter(x(:), y(:), 10, velo(:))
You might potentially also want to add a caxis() call, if you want to map velocities to consistent colors.
##### 0 个评论显示隐藏 -1更早的评论

Image Analyst 2022-6-5

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.
colormap(cmap);
minVelocity = 0
maxVelocity = max(velo(:))
colorbar;
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:

### 类别

Find more on Orange in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by