Can i get 12 different colors for 12 different variables in matlab? So far I could only get 6 colors because i don't know the values for the other colors:

if(strcmp(matrix(i,2),"C1")==1)
redundant sRGB matrix values replaced with ...
end
fs = 10;
score = repmat(linspace(1,6,12).',[1 3]);
matrix = ["" "C1"; "" "C2"; "" "C3"; "" "C4"; "" "C5"; "" "C6"; ...
"" "C7"; "" "C8"; "" "C9"; "" "C10"; "" "C11"; "" "C12"];
lb = strcat(repmat("Test string ",[12 1]),matrix(:,2));
%I am trying to classify each C by a different color but I only know 6 color numbers without adding decimals. Is there a way to
%add
%12 different colors in this format? i.e. [1.5, val = 0, 1.2]

1 commentaire

"Can i get 12 different colors for 12 different variables in matlab?"
You can generate distinguishable colors using my function MAXDISTCOLOR:

Connectez-vous pour commenter.

 Réponse acceptée

I'm not really sure what you're asking for or why. The thing you have is a set of repeated pairs. The thing you're asking for [1.5 0 1.2] isn't a valid unit-scale color tuple. Is val always zero? If not, will it also exceed [0 1]? Is there some particular meaning to the sequence and scale of the colors? If so, we'd obviously need to know.
If you just need 12 unique colors, you can use one of the colormap generator functions like jet(), parula(), etc to generate a map of a specified length. If you want to group them in pairs, you can use something like the attached function.
The rest of this can be cleaned up to eliminate the repeated code. Normally, you can use a switch-case structure instead of repeated if-else tests to do this, but in this case, the conditionally-executed code is identical except for one parameter.
% some placeholder inputs
fs = 10;
score = repmat(linspace(1,6,12).',[1 3]);
matrix = ["" "C1"; "" "C2"; "" "C3"; "" "C4"; "" "C5"; "" "C6"; ...
"" "C7"; "" "C8"; "" "C9"; "" "C10"; "" "C11"; "" "C12"];
lb = strcat(repmat("Test string ",[12 1]),matrix(:,2));
% generate a color table of appropriate length
colortable = tab20(12);
% demonstrate
xlim([0 10])
ylim([0 7])
hold on
for i = 1:12
% just get the trailing numeric part
thisclass = str2double(strrep(matrix(i,2),'C',''));
% print the text
text(score(i,1),score(i,2),score(i,3),lb(i), ...
'Color',colortable(thisclass,:),'fontsize',fs,'FontWeight','bold');
end
The attached function is from here. There are many other similar tools on the File Exchange.

9 commentaires

Yes val is always zero.
The code i wrote basically groups C1 and C2 together in the same color (because I did not know the value for 12 seperate colors only 6 colors in matlab) otherwise it would be 12 different colors.
Can you show how i would implement the 12 colors for each C1-C12? I.e. is there a [0,1] that can substitute into the pairs i have to show what you have in my loop? Thanks! @DGM
The example I gave generates 12 colors (the color table), and the demo code inside the loop replaces the entire if-else structure you were using.
If you want the 12 colors to be different than the ones I picked, that's fine. You could use any other colormap tool, like jet(12) or something.
If you want them to be based on the colors you had selected, the question is how. Consider the possible interpretations:
I could do simple linear interpolation between the colors
% linear interpolation in sRGB
colortable = [0 0 1;
0 0 0.545;
0 0 0.0909;
0.364 0 0;
0.818 0 0;
0.727 0.273 0;
0.273 0.727 0;
0 1 0.182;
0 1 0.636;
0.0909 0.909 1;
0.545 0.455 1;
1 0 1];
Though that gets a bit muddy and ambiguous. You'll probably get better results doing that in linear RGB.
% linear interpolation in linear RGB
colortable = [0 0 1;
0 0 0.765;
0 0 0.333;
0.637 0 0;
0.915 0 0;
0.869 0.559 0;
0.559 0.869 0;
0 1 0.464;
0 1 0.819;
0.333 0.959 1;
0.765 0.705 1;
1 0 1];
Notice how this is a bit of a fencepost problem. If we subdivide the intervals between the 6 colors, we end up with 11 colors, not 12. Subdividing the overall span into 12 colors means that only the first and last of the original colors is included exactly in the result. One way around this might be to make the map circular. Note how this map includes all the original six.
% linear interpolation in linear RGB (circular)
colortable = [0 0 1;
0 0 0.735;
0 0 0;
0.735 0 0;
1 0 0;
0.735 0.735 0;
0 1 0;
0 1 0.735;
0 1 1;
0.735 0.735 1;
1 0 1;
0.735 0 1];
Alternatively, instead of interpolating, maybe we just want six pairs of colors based on the original six. For sake of uniformity and readability against a white background, I'm going to assert that each pair has approximate luma of [0.4 0.6].
% alternating luma
colortable = [0.354 0.354 0.754;
0.549 0.549 1;
0.4 0.4 0.4;
0.6 0.6 0.6;
0.68 0.28 0.28;
1 0.429 0.429;
0.165 0.565 0.165;
0.248 0.848 0.248;
0.12 0.52 0.52;
0.179 0.779 0.779;
0.635 0.235 0.635;
0.952 0.352 0.952];
If one of those color tables suits your needs, you can copy and paste it. If you want to know how I generated those from the given six colors:
% do linear interpolation in sRGB
CT = [0 0 1;
0 0 0;
1 0 0;
0 1 0;
0 1 1;
1 0 1]
newct = interp1(linspace(0,1,6),CT,linspace(0,1,12));
mat2str(newct,3) % dump a pastable copy of the CT
imagesc(ctflop(newct)) % view the new colors
imwrite(imresize(ctflop(newct),[120 120]*2,'nearest'),'ctswatch.png')
% do linear interpolation in linear RGB
CT = [0 0 1;
0 0 0;
1 0 0;
0 1 0;
0 1 1;
1 0 1]
% this is being done in linear RGB
% i'm skipping the rgb2lin() step since it won't matter for these inputs
newct = interp1(linspace(0,1,6),CT,linspace(0,1,12));
newct = lin2rgb(newct);
mat2str(newct,3) % dump a pastable copy of the CT
imagesc(ctflop(newct)) % view the new colors
imwrite(imresize(ctflop(newct),[120 120]*2,'nearest'),'ctswatch.png')
% do linear interpolation in linear RGB (circular
CT = [0 0 1;
0 0 0;
1 0 0;
0 1 0;
0 1 1;
1 0 1]
% make the map circular
newct = interp1(linspace(0,1,7),CT([1:end,1],:),linspace(0,1,13));
newct = lin2rgb(newct);
newct = newct(1:end-1,:);
mat2str(newct,3) % dump a pastable copy of the CT
imagesc(ctflop(newct)) % view the new colors
imwrite(imresize(ctflop(newct),[120 120]*2,'nearest'),'ctswatch.png')
% make a variant where each color in the original table
% is represented as a pair of colors in the same hue
% but with a predefined luma
Y = [0.4 0.6]; % predefined luma
CT = [0 0 1;
0 0 0;
1 0 0;
0 1 0;
0 1 1;
1 0 1]
CT = kron(CT,[1; 1]);
Y = repmat(Y(:),[6 3]);
newct = imblend(ctflop(CT),ctflop(Y),1,'colorhsvyc');
newct = ctflop(newct);
mat2str(newct,3) % dump a pastable copy of the CT
imagesc(ctflop(newct)) % view the new colors
imwrite(imresize(ctflop(newct),[120 120]*2,'nearest'),'ctswatch.png')
imblend() and ctflop() are part of MIMT, but ctflop() can be replaced with permute(...,[1 3 2])
lin2rgb() is part of Image Processing Toolbox.
Ok I’m going attempt to replace it with the for loop color table you made. Thanks. I just wanted 12 distinct colors that can be easily visible to those who might be color blind or hard of seeing.
"I just wanted 12 distinct colors that can be easily visible to those who might be color blind or hard of seeing"
Generating maximally/reasonably distinct colors is not a trivial task, as it essentially involves reverse-engineering the color-perception behavior of the human eye and brain. You can find some attempts on the internet and on FEX:
Your requirement for 12 distinguishable colors would be fairly easy for "normal" vision, but is practically impossible once you take into account the three common forms of colorblindness: "Beyond 8, it is close to impossible to find colors that can be readily distinguished."
I would be very surprised if you could find a palette with 12 distinguishable colors that is simultaneously "easily visible to those who might be color blind or hard of seeing", and that is representable on the limited range of modern LCD monitors. If you find such a thing please post a link here, I would be most interested.
"I’m going attempt to replace it with the for loop color table you made."
Which does not fulfil your requirements at all: none of the colormaps DGM used or created make any attempt to ensure distinctiveness for colorblind users (because you did not specify this in your question).
You can find a nice palette here, which works for the common forms of colorblindness:
Note how it has only 8 colors.
Yeah. The examples I gave were far more simplistic than what those requirements would need. Stephen pretty much said it all, and I can recommend maxdistcolor().
"I can recommend maxdistcolor()."
Thank you. Note that MAXDISTCOLOR does not include any processing to account for colorblind limitations.
@Stephen23 @DGM Thank you for this wonderfully clear and detailed explanation. I have a deeper understanding and appreciation for your experience and this matlab capability. Thank you.
-Chanille
@Chanille: i highly recommend watching this video about the developing the new MatPlotLib colormaps:
It is an compact, entertaining, yet very educational introduction to the world of color perception modelling.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by