Change the colors of a bar histogram in Scilab

Viewed 47

I have a 40*2 matrix and I want to represent it in a bar format in SCILAB. And i want the data to be stacked.

So I checked and used the bar function, and for now it goes like this :

bar(Data,'stacked');

stacked bar histogram for a 40*2 matrix in green and blue

I want to customize the color of the graph by giving differents pairs of colors for each bar of the plot.

I tried to use a "vector of M strings" like suggested on the scilab help, but it takes into account only the first two colors specified, as I expected.

bar(Data,['yellow','red','cyan,'black'],'stacked']

stacked bar histogram for a 40*2 matrix, in red and yellow

Does anyone here might have a clue as to how I can do that ? Thanks a lot in advance

EDIT : So thanks to S. Gougeon i can do it. But now I want to use the barh() function instead of the bar(). I tried to change only the bar by barh, but obviously it didn't work. I tried then to do it with a single bar :

y=[40 60]; barh(1,y,'stacked');

I get the following image and these warning messages : WARNING: Transposing row vector Y to get compatible dimensions WARNING: Transposing data matrix Y to get compatible dimensions bar chart with one horizontal bar that goes from 40 to 100

I don't know why I have those warnings, since it's correctly working with bar(); and there is no difference in the help for the barh() function. Moreover there is only the second data on the chart, and I still don't know why.

2 Answers

If you want to change the pair of colors for any bar to the next one, you have to plot the bars one by one. Here is an example:

clf
n = 30;
colors = ["blue" "magenta" "green" "cyan" "red" "yellow" "pink" "linen" "bisque" "scilabcyan3" "lavender" "deepskyblue" "slateblue" "skyblue" "turquoise" "palegreen" "white" "scilabmagenta3" "scilabbrown2" "scilabpink4" "khaki" "indianred"];
C = colors(grand(n,2,"uin", 1,size(colors,"*")));

y = rand(1,n)*100;
y = [y ; 100-y]';
drawlater
for i = 1:n
    bar(i, y(i,:), 0.8, C(i,:), "stacked");
end
gca().x_ticks = tlist(["ticks" "locations" "labels"],1:n, string(1:n));
gca().tight_limits(1) = "on";
drawnow

enter image description here

For the barh() issues:

  • please note that warnings have already been reported.
  • do not hesitate to report the bad display

This one can be worked around by using bar(), and then rotate the axes by 90°:

gca().rotation_angles(2) = 0;

enter image description here

Related