Scatter with colour-coded markers (matrix)

조회 수: 102 (최근 30일)
Elli
Elli 2022년 6월 5일
댓글: Adam Danz 2022년 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?
Thanks for your help!

답변 (3개)

Lateef Adewale Kareem
Lateef Adewale Kareem 2022년 6월 5일
편집: Lateef Adewale Kareem 2022년 6월 5일
minv = min(min(velo));
maxv = max(max(velo));
c = (velo - minv)./(maxv - minv);
scatter(x(:),y(:),10,c(:))
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 6월 5일
This is not needed, scatter uses caxis mapping. And if it were necessary then use rescale() or the older mat2gray()
Adam Danz
Adam Danz 2022년 6월 5일
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
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.

Image Analyst
Image Analyst 2022년 6월 5일
편집: 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:

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by