MATLAB: How to select different colors and legend for the bar plot?

MATLABplotplotting

Hello everyone, I plot this bar plot in order to show the difference between each model (3 models) in condition A and condition B. x = rand(3,2);
x = rand(3,2);
h = bar (x);
I want to set, for example, red for the first model in condition A and blue for condition B (In this model).
As well as two different colors like green and yellow for the second model in each condition. Like this plot below:
And also, set the corresponding legend for each color. So I want to have 6 items in legend (different colors).
Any suggestion is really helpful
Thank you.

Best Answer

For the first part of your question, you can do so as follows:
x = rand(3,2);
h = bar (x);
h(1).FaceColor = 'red'
h(2).FaceColor = 'blue'
For the second part, patterned face colours aren't provided for with the bar() function. When you plot a bar chart such as:
h = bar(rand(3,6))
you can continue to modify the colors of any model using the same procedure as above. Additionally the legend will update for you accordingly:
legend('model A','model B','model C','model D','model E','model F')