How to change a certain value color in pcolor

29 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Orange 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by