Filter löschen
Filter löschen

How to change a certain value color in pcolor

29 Ansichten (letzte 30 Tage)
Shan  Chu
Shan Chu am 23 Jun. 2016
Kommentiert: Jitesh Dadich am 25 Feb. 2020
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 Kommentare
Walter Roberson
Walter Roberson am 24 Feb. 2020
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 am 25 Feb. 2020
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 !

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Thorsten
Thorsten am 23 Jun. 2016
Bearbeitet: Thorsten am 23 Jun. 2016
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 Kommentar
Guillaume
Guillaume am 23 Jun. 2016
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 23 Jun. 2016
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

Kategorien

Mehr zu Orange finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by