create specific color map and save it
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
how do i create my own color map for 2D-scatter plot.
such that all the values of z
- z <= 10 ------- take green color
- 10 < z <= 25 ----- yellow color
- 25 < z <= 50 ----- orange color
- z > 50 ----- red color
i want to create a color map obeying such condition and save it , so that i can use with whatever z i obtain in future.
as an example i have attached x,y,z
scatter(x,y,[],z,'filled','o')
1 件のコメント
KSSV
2020 年 5 月 28 日
Same question you have asked multiple times...
採用された回答
Ameer Hamza
2020 年 5 月 28 日
Try this
val_z = (10:51).';
idx = 1*(val_z<=10) + ...
2*((10<val_z) & (val_z<=25)) + ...
3*((25<val_z) & (val_z<=50)) + ...
4*(val_z>50);
colors = [0 1 0; % green
1 1 0; % yellow
1 0.5 0; % orange
1 0 0]; % red
cmap = colors(idx, :);
scatter(x,y,[],z,'filled','o')
colormap(cmap)
set(gca, 'CLim', [10 51]);
12 件のコメント
Sajid Afaque
2020 年 5 月 28 日
as i am new to this concept,can you please explain me the above code.
Ameer Hamza
2020 年 5 月 28 日
This uses basic indexing operations available in MATLAB. It is difficult to summarize everything in one comment. I suggest running this code line by line and see the output of each step. Also, read about the indexing from the following links
Sajid Afaque
2020 年 5 月 28 日
First of al thank you for your help.
i am getting the colors according to my conditions.
but my color bar is showing up weird.

why is orange color repeating itself to the position of its value. and why don't green and yellow show up in color bar.
Additionally ,what should to i do to have increase in the darkness of color as value increases.( for example value > 50 we have red color, how can we acheive such that 51 has light red compared to 55 which should again be light red compared 60 and so on, my max value should be dark red)
Ameer Hamza
2020 年 5 月 28 日
Try this
val_z = (10:max(z)).';
cmap = zeros(numel(val_z), 3);
mask = val_z<=10;
cmap(mask, :) = repmat([0 1 0], sum(mask), 1); % green
mask = (10<val_z) & (val_z<=25);
cmap(mask, :) = repmat([1 1 0], sum(mask), 1); % yellow
mask = (25<val_z) & (val_z<=50);
cmap(mask, :) = repmat([1 0.5 0], sum(mask), 1); % orange
mask = val_z>50;
n = sum(mask);
t = linspace(0, 1, n).';
red = [1 0 0];
dark_red = [0.3 0 0];
cmap(mask, :) = (1-t).*red + t.*dark_red;
scatter(x,y,[],z,'filled','o')
set(gca, 'CLim', [10 max(z)]);
colormap(cmap)
colorbar
Sajid Afaque
2020 年 5 月 28 日

I am getting this error
Ameer Hamza
2020 年 5 月 28 日
Which MATLAB version are you using?
Sajid Afaque
2020 年 5 月 28 日
2013b
Ameer Hamza
2020 年 5 月 28 日
You can try this
val_z = (10:max(z)).';
cmap = zeros(numel(val_z), 3);
mask = val_z<=10;
cmap(mask, :) = repmat([0 1 0], sum(mask), 1); % green
mask = (10<val_z) & (val_z<=25);
cmap(mask, :) = repmat([1 1 0], sum(mask), 1); % yellow
mask = (25<val_z) & (val_z<=50);
cmap(mask, :) = repmat([1 0.5 0], sum(mask), 1); % orange
mask = val_z>50;
n = sum(mask);
t = linspace(0, 1, n).';
red = [1 0 0];
dark_red = [0.3 0 0];
cmap(mask, :) = interp1([0 1], [red; dark_red], t);
scatter(x,y,[],z,'filled','o')
set(gca, 'CLim', [10 max(z)]);
colormap(cmap)
colorbar
Sajid Afaque
2020 年 5 月 28 日

again my colorbar shows green at higher values whereas it should have been limited to trange 0-10
Ameer Hamza
2020 年 5 月 28 日
There seems to be some difference in the working of colormap in R2013b. In R2020a, it looks fine

Sajid Afaque
2020 年 5 月 28 日
yeah color ranges are ok ,only colorbar gets repeated after certain interval.
do you have any idea why this may be.
thank you for your help .
much appreciated
Ameer Hamza
2020 年 5 月 28 日
I don't have any idea about this. Maybe something is mentioned in archived documentation, but the reason is not obvious.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および 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)
