Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color in grouped barplot in Matlab

This is my matrix

n =

   46.4000   51.8000
   44.8000   44.9000
   67.2000   85.0000
   54.4000   60.3000
   43.2000   57.0000
   51.2000   68.0000
   75.2000   76.0000
   44.8000   51.3000
   67.2000   72.2000
   70.4000   71.2000

If I plot it like bar(n, 'grouped') it shows

BarPlot

I want to change the default colors instead of blue red I want green and yellow

I tried like this way bar(n, 'grouped','g','y') But it is showing
secondplot

can any one help me ?

like image 887
iAnas Avatar asked Jun 16 '14 10:06

iAnas


People also ask

How do I change the color of one bar in a bar graph in Matlab?

By default, when you create a bar chart, the CData property contains a three-column matrix of RGB triplets. You can change the color for a particular bar by changing the corresponding row in the matrix. This property applies only when the FaceColor or EdgeColor property is set to 'flat' .

How do I change the color of my barplot?

You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.


1 Answers

You can use the 'FaceColor' property of the handles to the objects:

n=[46.4000   51.8000
   44.8000   44.9000
   67.2000   85.0000
   54.4000   60.3000
   43.2000   57.0000
   51.2000   68.0000
   75.2000   76.0000
   44.8000   51.3000
   67.2000   72.2000
   70.4000   71.2000];
bar_handle = bar(n,'grouped');
set(bar_handle(1),'FaceColor',[0,1,0])
set(bar_handle(2),'FaceColor',[1,1,0])

The documentation explains how to set the colors here.

like image 146
darthbith Avatar answered Sep 20 '22 16:09

darthbith