MATLAB Colormap doesn't work past 12 colors

13 Ansichten (letzte 30 Tage)
Meghna Wagley
Meghna Wagley am 5 Jun. 2020
Bearbeitet: Stephen23 am 6 Jun. 2020
I have a custom colormap in my code
S = [0, 0, 1 % 1: blue
0, 1, 0 % 2: green
1, 1, 0 % 3: yellow
1, 0, 0 % 4: red
1, 1, 1 % 5: white
1, 0.75, 0.79 %6
0.5, 0, 0 % 7: Maroon
0.86, 0.07, 0.23 % 8 crimson
0.1, 0.75, 0.79 % 9 dark red
0, 0.74, 1 %10 light sky blue
1, 0.75, .79 %11 pink
];
I tried to add more colors to this, but if there are more than 12 colors, the entire map gets messed up. For example, when there are 14 colors in the map, even if the extra two are unused, whatever I try to color white becomes light blue, red becomes white, etc. I don't understand why this is happening.
Also, I tried to create a new colormap but it doesn't work. I even tried renaming this map from S to another variable name, and changed the name throughout the whole code and got the error message "Error using colormap (line 61)
First argument must be a scalar axes or figure handle."
Why can I not add more colors to the original colormap, and why can I not rename it?
Thanks !

Antworten (1)

Stephen23
Stephen23 am 5 Jun. 2020
Bearbeitet: Stephen23 am 6 Jun. 2020
" when there are 14 colors in the map, even if the extra two are unused..."
That depends on what you are doing.
Depending on the plotting function you are using it might be scaled to fit the current colormap, regardless of how many colors it has. So all of the colors, from first to last, will be used.
If you are working image etc. then the default colormapping is direct, so each pixel value corresponds to one colormap value. No scaling involved. Then you might end up with "unused" colors.
As you do not actually given any information on what kind of plotting commands you are using, it is hard to give specific code or advice.
"... whatever I try to color white becomes light blue, red becomes white, etc. I don't understand why this is happening."
That sounds like the data is being scaled to the colormap, but the "unused" colors sounds like direct mapping.
If you do not want automatic scaling of the colormap, then you can set the colormap limits yourself using caxis.
  3 Kommentare
Stephen23
Stephen23 am 6 Jun. 2020
Bearbeitet: Stephen23 am 6 Jun. 2020
Your colormap includes white twice, on rows 5 and 16.
If you want to specify exact colors for specific values by far the easiest approach is to use direct mapping, e.g.:
>> S = [0,0,1;0,1,0;1,1,0;1,0,0;1,1,1;1,0.75,0.79;0.5,0,0;0.86,0.07,0.23;0.1,0.75,0.79;0,0.74,1;1,0.75,0.79;0,0,0.6;0.6,0,0.6;0,0.6,0.6;0,0,0;1,1,1;1,0.5,0;0.8,0.6,1;1,0,1;0.2,0.4,0;1,0,0.5;0.63,0.63,0.63;0.4,0.2,0;1,0.9,0.8;1,0.6,0.8];
>> A = [1,2,3;4,5,6]; % 5==white
>> image(A)
>> colormap(S)
giving this figure (note how the data values 1 to 6 map directly to the colormap rows 1 to 6):
If you want to map to other colors (i.e. scale the data to fit all/part of the colormap) then you will need to calculate the relative positions of the colors in the colormap. For example, lets now use the other white in your colormap, and the colors adjacent to it, e.g. rows 12 to 17 of your colormap. An easy way to get the correct caxis scaling is to use interpolation/extrapolation to find the required data values e.g.:
>> image(A,'CDataMapping','scaled')
>> colormap(S)
>> L = interp1([12,17],[1,6],[1,25],'linear','extrap')
L =
-10 14
>> caxis(L)
Where the inputs to interp1 are:
  • [12,17] the required colormap row indices
  • [1,6] corresponding to these actual data values
  • [1,25] the size of the colormap (i.e. first and last row)
and the output L gives the data range (which might not correspond to any actual data values) required to map the entire colormap so that your requested subset of colors maps to the specified data values.
Walter Roberson
Walter Roberson am 6 Jun. 2020
"5" used to correspond with white but now that the colors have been extended, should I try something like "2" for white?
Under the assumption that your colormap is being mapped to the data range (rather than to fixed values), then to get white, use 5/size(S,1) as the data value.
Some utilities such as imshow() look at the data type of the input and use it to automatically determine the color mapping to use. For example if you had uint8 data that was in the range 1 to 100, then imshow() would by default assume that your valid input is 0 to 255 and that you just didn't happen to have any values in the 101 to 255 range. By default, imshow would not look at the data and see the 1 to 100 actual data and set up 1 to map to the first color and 100 to map to the last color. You can pass a scaling window to imshow, including imshow(ARRAY, []) to force imshow to examine the actual data range, but by default it does not do so.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by