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

2 commentaires

Does regular plotting and applying a colourmap not give what you want?
e.g.
A = magic(25);
figure; imagesc( A );
colorbar
You can certainly do mapping onto a colourmap manually and produce a true RGB image if you wish, but it is obviously more effort and data cursor information would reflect that it is now RGB rather than just give you the raw value.
Alternatively you can manipulate your data in some way if a straight linear mapping onto the colourmap is not what you want. Or create a custom colourmap.
Hard to say without seeing an example though.
Harish Rajan
Harish Rajan le 24 Mar 2020
But the point is I dont want to have an RGB image. The objective is to create a vector with colour values corresponding to the energy values, so that i can plot them normally using the plot command and when a issue a fill command for the markers, instead of all them being same colour they follow the one given by the colour map.

Connectez-vous pour commenter.

 Réponse acceptée

J. Alex Lee
J. Alex Lee le 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?

5 commentaires

Harish Rajan
Harish Rajan le 24 Mar 2020
can I then use the plot function with this greymap or xcolors vector, x and y coordinates vectors to plot the values and fill in the appropriate colour for the markers ?
J. Alex Lee
J. Alex Lee le 24 Mar 2020
This is why it's best to have example data...i just arbitrarily called the index-able data "x", but do you mean this is also actually the x-coordinate data?
Assuming you have 2 coordinates per point (x and y), and then your actual indexed data (say, z), then I think you can do what you want with scatter(). If you have 3 coordinates (x, y, z), and you have actual indexed data, then you can probably use scatter3(). Full disclosure, I don't have much experience with scatter or scatter3.
theta = linspace(0,(2*pi),30);
x=cos(theta);
y=sin(theta);
energy = linspace(0,1000,30);
Lets say here i have 30 points with x and y coordinates and a energy vector with same number of elements as the numbers of points. Now i want to create colormap (lets say using the method you suggested). Once i have done it I want to plot there points, but instead of each marker having the same colour they will have the colour corresponding to their corresponding value in energy vector. Thus the last point in the example above will have a solid black colour and the first will be white.
J. Alex Lee
J. Alex Lee le 24 Mar 2020
Modifié(e) : J. Alex Lee le 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
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

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 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);

4 commentaires

Harish Rajan
Harish Rajan le 24 Mar 2020
Thank you very much, i think both yours and J alex's answers work very well
Harish Rajan
Harish Rajan le 30 Mar 2020
Can also add marker size scaling and colourmap ? Can it be done in the above example or do i need to write a seperate code for it ?
Image Analyst
Image Analyst le 30 Mar 2020
Harish, you can see how I defined the colormap before the loop. You can make it be anything you want. You can also see how I said the 'MarkerSize' was 40 in the call to plot(). You can change that 40 to be whatever you want. It can even be a function of k, the loop iterator if you want.
Harish Rajan
Harish Rajan le 30 Mar 2020
Thank You

Connectez-vous pour commenter.

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by