9 views (last 30 days)

Hello,

I am trying to map a vector containing energy of modes of a dynamic system to a vector containing colour values so that when i plot this I want the markers to have the colour corresponding to their energy levels. For example the highest energy value will correspond to a Solid black colour and the next one a little less black then the others have grey and the least energy ones have white colour (thus making them invisible in the plot).

Best Regards

Harish

J. Alex Lee
on 24 Mar 2020

A brute force way would be to interpolate...not sure if there's a better way

x = % ... data vector

indices = 0:255;

xref = linspace(min(x),max(x),length(indices));

indexed_x = interp1(xref,indices,'nearest');

% then you can map to whatever colormap you want, e.g.

graymap = gray(length(indices));

xcolors = graymap(indexed_x,:);

or something like that?

J. Alex Lee
on 24 Mar 2020

upon looking at docs of scatter(), i see that it is doing more or less exactly what you want, AND it can use the axes underlying colormap, to Adam's point ( you don't have to manually index). Take a look at the docs https://www.mathworks.com/help/matlab/ref/scatter.html#d120e1106626

scatter(x,y,sz,energy)

colormap gray

J. Alex Lee
on 24 Mar 2020

Just for completeness, Image Analyst's full example but with using scatter:

theta = linspace(0,(2*pi),30);

x=cos(theta);

y=sin(theta);

energy = linspace(0,1000,30);

figure(1);

scatter(x,y,120,energy,'filled')

grid on

axis square

colormap jet

% to do the black and white example:

% colormap gray

Sign in to comment.

Image Analyst
on 24 Mar 2020

Do you mean where the marker color is the color taken from the "energy" array, like this:

% Define our data.

theta = linspace(0,(2*pi),30);

x=cos(theta);

y=sin(theta);

energy = linspace(0,1000,30);

numPoints = length(x);

% Get a colormap, a unique color for every energy level

cmap = jet(numPoints); % Initialize jet colormap.

% Get energy in the range 1 to numPoints so we can use that to get a row from the colormap.

qEnergy = imquantize(energy, numPoints);

for k = 1 : numPoints

% Get the color for this energy level:

thisEnergy = qEnergy(k);

thisColor = cmap(thisEnergy);

fprintf('Plotting point #%d at (%.3f, %.3f) with color (%.3f, %.3f, %.3f)\n',...

k, x(k), y(k), cmap(k, 1), cmap(k, 2), cmap(k, 3));

plot(x(k), y(k), '.', 'Color', cmap(k, :), 'MarkerSize', 40);

hold on;

end

grid on;

axis square;

fprintf('Done running %s.m ...\n', mfilename);

Image Analyst
on 30 Mar 2020

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/512627-how-to-map-a-vector-to-a-colourmap#comment_814574

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/512627-how-to-map-a-vector-to-a-colourmap#comment_814574

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/512627-how-to-map-a-vector-to-a-colourmap#comment_814577

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/512627-how-to-map-a-vector-to-a-colourmap#comment_814577

Sign in to comment.