How to change a certain value color in pcolor

42 ビュー (過去 30 日間)
Shan  Chu
Shan Chu 2016 年 6 月 23 日
コメント済み: Jitesh Dadich 2020 年 2 月 25 日
Dear all, I got an random matrix 1000-by-1000,varying from 1 to 10. I am using pcolor as below:
A=randi(10,1000)
figure
pcolor(A)
shading flat
h = colorbar;
ylabel(h, 'IM( \kappa )')
colormap jet
I want to change to color of the element of 5 to pink. How should I do? Thanks
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 2 月 24 日
Jitesh:
You will need to create a colormap with 81 entries. Row K of the colormap would correspond to the range starting at (K-1)*0.05 - 2.0 and extending for 0.05. This presumes that the highlight can be handled through a single color; if for example you need 0 to 0.01 to be a different color than 0.01 to 0.02 then you would need 4/0.01 + 1 = 401 colormap entries. Caution: older versions of Windows were restricted to 256 colors in a colormap.
The technique can change in the case where you want all the values < 0.0 to be a single color and all the ones above 0.05 to be a single color: using the large number of entries in the colormap is only necessary if you need color to work normally outside the range that you want to color specially.
Jitesh Dadich
Jitesh Dadich 2020 年 2 月 25 日
Hi Walter,
I already tried colormap with 81 entries with given codes below.
cmap = colormap;
N = 81;
x = linspace(1, size(cmap, 1), N);
cmap = cmap(x, :);
But it showed an error:
Subscript indices must either be real positive integers or logicals.
Error in PcolorTestfile (line 13)
cmap = cmap(x, :);
Could you help with script !

サインインしてコメントする。

採用された回答

Thorsten
Thorsten 2016 年 6 月 23 日
編集済み: Thorsten 2016 年 6 月 23 日
cmap = colormap;
N = 10;
x = linspace(1, size(cmap, 1), N);
cmap = cmap(x, :);
pink = [255 204 221]/255;
cmap(5,:) = pink;
colormap(cmap)
pcolor(A)
To change back to the default colormap:
colormap('default')
  1 件のコメント
Guillaume
Guillaume 2016 年 6 月 23 日
Note that matlab does not use linspace to calculate the mapping between cdata and colormap, so the above reduction of the number of colours in the colormap may not use exactly the same colours. (Granted it's not going to be off by much).
See my answer for the exact formula used for the mapping.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2016 年 6 月 23 日
When you call colormap jet you assign a colormap to the figure with as many entries as the default colormap, which is probably 64. Therefore, when matlab plots the pcolor, it does a mapping from the range of values in the array to 1 to 64, according to this formula (see here)
colourindex = fix((A-cmin)/(cmax-cmin)*m)+1;
where cmin is 1, cmax is 10 and m is 64, in your case.
So you could use this formula the find the row of the colour map that corresponds to 5 (it's row 29) and change that.
cmap = jet(64)
cmap(29, :) = [0.8 0.3 0.6]; %some sort of pink
colormap cmap
Or simpler, define a colormap with only 10 entries
cmap = jet(64);
cmap = cmap(fix(([1:10] - cmin)/(cmax-cmin)*64)+1);
cmap(5, :) = [0.8 0.3 0.6]; %some sort of pink
colormap cmap

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by