1
votes

I want to change the colors of my stacked bar plot but I keep getting an error message

As I need specific colors in my stacked bar plot to match with another graph I set up the color vector: colori with RGB values which I then convert into values between 0 and 1 after creating the figuer I want to change colors of the 15 stacked bars like this:

 for i = 1:15
    barSNE(i,:).FaceColor = colori(i);
 end

here a bit mor of the code:

...

colori =  [139,0,0
    255,160,122;
    255,69,0;
    255,165,0;
    255,215,0;
    154,205,50;
    34,139,34;
    50,205,50;
    255,182,193;
    106,90,205;
    139,0,139;
    32,178,170;
    199,21,133;
    30,144,255;
    0,0,205];
colori = colori ./ 255;   

ctMeansT = ctMeans.';

figure(2)
barSNE = bar(ctMeansT, 'stacked');

 for i = 1:15
    barSNE(i,:).FaceColor = colori(i);
 end

However, I get following Error message:

Structure assignment to non-structure object.

Error in viSNE_stacked_bar_plot (line 41)
     barSNE(i,:).FaceColor = colori(i);

Colors in the figuere are not changed as expected

1

1 Answers

0
votes

Your did a wrong indexing of your arrays barSNE and colori. If I run the code as you posted it above and check the size of both arrays I get

>> size(barSNE)

ans =

    1    15

>> size(colori)

ans =

    15     3

So barSNE is a one-dimensional array, while colori is a matrix of size 15 x 3. However, you tread it in your code vice versa given your index notation. E.g. If you do colori(i) you do not get the first RGB triplet as desired, but the first single value of the array colori (in your case 139/256 = 0.5451).

Change the indexing within the loop as follows

for i = 1:15
    barSNE(i).FaceColor = colori(i,:);
end

and it works. As an example with random numbers for ctMeans (since you haven't provided your original data) I get a bar chart like this

enter image description here