You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to convert a vector to rgb colormap?
16 views (last 30 days)
Show older comments
Hello guys
can anybody tell me how to convert a ector to rgb colormap??
I have have vector, which its value goes from -1 to 1. I want the define a colormap, where the value of the 0.2 to 1 is red and -0.2 to -1 is blue and the rest is green.
thanks in advance.
Accepted Answer
Image Analyst
on 30 Dec 2019
Do this:
% Colormap "where the value of the 0.2 to 1 is red and
% -0.2 to -1 is blue, and the rest is green.
numColors = 256;
vec = linspace(-1, 1, numColors);
cmap = zeros(numColors, 3);
% Make everything green to start with
cmap(:, 2) = 1;
% Make red from where vec goes from 0.2 to 1.
redIndexes = vec >= 0.2 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
% Make blue from where vec goes from -1 to -0.2.
blueIndexes = vec >= -1 & vec <= -0.2;
cmap(blueIndexes, 1) = 0;
cmap(blueIndexes, 2) = 0;
cmap(blueIndexes, 3) = 1;
cmap % Echo to command window so we can see it.
19 Comments
Image Analyst
on 31 Dec 2019
Ali's "Answer" moved here as a comment since it's a reply to me rather than an Answer to his original question:
thanks, but how can use it now as colormap?
Image Analyst
on 31 Dec 2019
How do you want to use it? Do you have an image displayed? If so, just call colormap(cmap)
colormap(cmap);
colorbar;
Ali Nouri
on 1 Jan 2020
Edited: Ali Nouri
on 1 Jan 2020
many thansk again!
But can you tell what is wrong with my loop?
why does not the color change?
for i=2:22
vec(:,i) = DD_vector{i-1}(:,1);
% Make red from where vec goes from 0.2 to 1.
if vec(:,i) >= 0.05 & vec(:,i) <= 1
% redIndexes = vec >= 0.05 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
elseif vec(:,i) >= -1 & vec(:,i) <= -0.05
% Make blue from where vec goes from -1 to -0.2.
% blueIndexes = vec >= -1 & vec <= -0.05;
cmap(blueIndexes, 1) = 0;
cmap(blueIndexes, 2) = 0;
cmap(blueIndexes, 3) = 1;
end
% Make everything green to start with
cmap(:, 2) = 1;
end
Image Analyst
on 1 Jan 2020
You're just assigning a variable. You never send that variable to the display with the colormap() function. You need to
colormap(cmap);
Ali Nouri
on 1 Jan 2020
numColors = 31;
vec = [-0.00653341206474312
0.00324578781459654
0.000552586889809205
0.0449766491863441
0.00356180692563616
0.0746463180472126
0.0078011233071506
-0.00078555536582595
-0.00374634887393066
0.00109938285489642
0.369351654077307
-0.00454936207691688
2.20408232101434e-05
0.000254609487960349
-0.00135086171535819
-0.0695803908468877
0.0116165165761393
-0.00197408951942686
-2.47282982787341e-05
0.375027165631663
-0.047418741927062
-0.840829144608918
-0.00254093170763818
0.0010078132834115
-0.000870537370561834
-0.019269131980187
0.000548413597787709
-0.00772253261710933
0.00372968305677465
0.0221712405360291
0.00246775692879843];
cmap = zeros(numColors, 3);
% Make everything white to start with
cmap(:, :) = 1;
% Make red from where vec goes from 0.2 to 1.
redIndexes = vec >= 0.02 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
% Make green from where vec goes from -1 to -0.2.
greenIndexes = vec >= -1 & vec <= -0.02;
cmap(greenIndexes, 1) = 0;
cmap(greenIndexes, 2) = 1;
cmap(greenIndexes, 3) = 0;
cmap; % Echo to command window so we can see it.
%B=sortrows((cmap));
%colormap(B);
colormap(cmap)
colorbar;
caxis([-1 1])
set(get(colorbar,'YLabel'),'String','risk')
As you can see that I use colormap(cmap), but something seems wrong, can you see what is wrong with the way i try to make plot.
Image Analyst
on 1 Jan 2020
Can you post that image and the code you used to display it? I don't know what you want. You changed what I did to make values white instead of green. It's doing what you told it to do. Are you sure each country has the value you expect it to?
Ali Nouri
on 1 Jan 2020
Edited: Image Analyst
on 1 Jan 2020
Which code??
Well I have 21 vector, that is why i used for loop to convert every vector to a new rgb colormap!
I didn't success to make code for it.
Walter Roberson
on 1 Jan 2020
You did not do the linspace like Image Analyst said to do.
Image Analyst
on 1 Jan 2020
And you didn't attach the image and code to show how you displayed it, even though I directly asked for it. Come on, make it easy for people to help you, not hard.
Ali Nouri
on 1 Jan 2020
Edited: Ali Nouri
on 1 Jan 2020
I sent you the code and picture of the result in my previous message and I ask you again for code an image, and I asked you what code and picture you are looking for? at least try to be a little precise when you are asking someone, who is not so much familiar with matlab.
Ali Nouri
on 1 Jan 2020
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
numColors = 31;
%value of 31 country
vec = [-0.00653341206474312
0.00324578781459654
0.000552586889809205
0.0449766491863441
0.00356180692563616
0.0746463180472126
0.0078011233071506
-0.00078555536582595
-0.00374634887393066
0.00109938285489642
0.369351654077307
-0.00454936207691688
2.20408232101434e-05
0.000254609487960349
-0.00135086171535819
-0.0695803908468877
0.0116165165761393
-0.00197408951942686
-2.47282982787341e-05
0.375027165631663
-0.047418741927062
-0.840829144608918
-0.00254093170763818
0.0010078132834115
-0.000870537370561834
-0.019269131980187
0.000548413597787709
-0.00772253261710933
0.00372968305677465
0.0221712405360291
0.00246775692879843];
cmap = zeros(numColors, 3);
% Make everything white to start with
cmap(:,:) = 1;
% Make red from where vec goes from 0.2 to 1.
redIndexes = vec >= 0.2 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
% Make green from where vec goes from -1 to -0.2.
greenIndexes = vec >= -1 & vec <= -0.2;
cmap(greenIndexes, 1) = 0;
cmap(greenIndexes, 2) = 1;
cmap(greenIndexes, 3) = 0;
cmap; % Echo to command window so we can see it.
B=sortrows((cmap),1);
colormap(B);
%colormap(cmap)
colorbar;
caxis([-1 1])
set(get(colorbar,'YLabel'),'String','risk')
% shaperead for country
land = shaperead('cntry02.shp', 'UseGeoCoords', true);
%map over europe
w = worldmap('europe');
setm(gca,'ffacecolor','b') %using this line the seas are colored in blue
%facecolor with new colormap
faceColors = makesymbolspec('Polygon',...
{'INDEX', [1 31], 'FaceColor', ...
colormap});
% geshow with new facecolor
geoshow(land,'DisplayType', 'polygon',....
'SymbolSpec',faceColors)
Walter Roberson
on 1 Jan 2020
numColors = 31;
cvec = linspace(-1, 1, numColors);
cmap = ones(numColors, 3); %defaults to white
redIndexes = cvec >= 0.05 & cvec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
blueIndexes = cvec >= -1 & cvec <= -0.05;
cmap(blueIndexes, 1) = 0;
cmap(blueIndexes, 2) = 0;
cmap(blueIndexes, 3) = 1;
Now display your image, and colormap(cmap)
Your vector of values, vec, has no role in this, unless it has some role in creating the values for the image you are displaying.
Walter Roberson
on 1 Jan 2020
Your colorbar looks like that because you did not use linspace() like we said. You used your unsorted vec, and you used one colormap entry for each entry in vec.
Is your task to create a colormap and apply it to an image (or imagem or surface plot or patch) ? If so then you need to create a sorted vector of equal-spaced values -- the linspace that we have been repeatedly using in the code -- and in that case you would get a sensible colormap.
Or is your task to take a vector of values and convert each one into an RGB color directly, such as you might do for creating an RGB image yourself without a colormap, or such as you might do for creating a scatter plot in which each point is individually colored? That is what your code in https://www.mathworks.com/matlabcentral/answers/498482-how-to-convert-a-vector-to-rgb-colormap#comment_782315 is doing, converting each value in the vector into an RGB color directly, with the resulting array of color entries being your cmap array. That is a valid thing to do, very useful sometimes, but it will not give you an array you can use for a colorbar.
It is valid to combine the two approaches, to create a direct array of RGB values, and to also create a linear colormap using linspace() that you use to colorbar(), just for user comprehension, even though the colormap is not influencing what displays on the screen in that case.
Ali Nouri
on 1 Jan 2020
My task as you mention; it is to take a vector of random value, which are between -1 and 1 and and the i need to convert each value to an rgb colormap. when i have convert this vector to rgb colormap, then i need use this color map to make plot over europe, beacuse each value in this vector represent for one country in europe.
I can sort the vector and change it to rgb colormap, but then the values in vector does not represent the right country.
Now with help of you guys, i can convert a vector to rgb colormap, but still i can not use this colormap correlty.
Image Analyst
on 1 Jan 2020
Sorry but I don't have the mapping toolbox so I can't run some functions in your code and don't have demo files such as 'cntry02.shp'. I've added the Mapping Toolbox to the Products list your were supposed to fill out when you posted this. Hopefully Wlater has it and can continue to help you.
Image Analyst
on 2 Jan 2020
Sine I don't have the toolbox, I don't have shaperead() and so I can't continue.
More Answers (0)
See Also
Categories
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)