Specify color to specific values in pcolor

18 views (last 30 days)
Greetings.
I have a matriz A whose elements are 1 of these 4 different values: 0, 1, 2 and 7 and I want each value to be assigned a color of my choosing. The elements change value (and color) in every time iteration. How do I do that?
I´m currently using:
0 = black [0 0 0]
1 = blue [0 0 1]
2 = yellow [1 1 0]
7 = green [0 1 0]
But I´m not getting the colors with
mymap=[0 0 1 ; 1 1 0 ; 0 0 0 ;0 1 0]
colormap(mymap)
So... how to assign color to specific values?

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2021
Edited: Walter Roberson on 17 Feb 2021
you would need 8 entries in the color map, because it does linear interpolation from values to color index.
However, pcolor() is the wrong tool. pcolor is really surf() and surf takes the values at four corners and interpolates the face colour from that. It does not map directly from array value to color.
For mapping array to color, imagesc() if you are good with a an equal-spaced result that is strictly a 2d image (disappears viewed from angles.) If you were using pcolor for transformed coordinates or getting a pane that did not disappear on angle, then instead use warp() https://www.mathworks.com/help/images/ref/warp.html which does texture mapping

More Answers (0)

Community Treasure Hunt

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

Start Hunting!