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
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
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
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
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Orange에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!