Search code examples
matlabmatlab-figure

Draw a figure by using ``bar'' with individual colors


An Example

I want to draw a picture in MATLAB like this.

Please see the following codes in MATLAB.

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];

h = bar(y);  

set(h, {'DisplayName'}, {'Jan','Feb','Mar'}')  % MATLAB tells me there is error here.

legend() 

Example

However, the colors for {'Jan','Feb','Mar'} are assigned automatically. Can I use the indivual colors? For example, red for 'Jan', blue for 'Feb', and yellow for 'Mar'.


Solution

  • Well, the code set(h, {'DisplayName'}, {'Jan','Feb','Mar'}') is correct. The single quotation mark after Mar'} is necessary. If we delete it, MATLAB cannot perform well.

    For using individual color, just use set(h(1),'Facecolor','r')