現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to convert a vector to rgb colormap?
13 ビュー (過去 30 日間)
古いコメントを表示
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.
採用された回答
Image Analyst
2019 年 12 月 30 日
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 件のコメント
Image Analyst
2019 年 12 月 31 日
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
2019 年 12 月 31 日
How do you want to use it? Do you have an image displayed? If so, just call colormap(cmap)
colormap(cmap);
colorbar;
Ali Nouri
2020 年 1 月 1 日
編集済み: Ali Nouri
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 1 日
編集済み: Image Analyst
2020 年 1 月 1 日
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.
Image Analyst
2020 年 1 月 1 日
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
2020 年 1 月 1 日
編集済み: Ali Nouri
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 1 日
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
2020 年 1 月 2 日
Sine I don't have the toolbox, I don't have shaperead() and so I can't continue.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Blue についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)