Thread Subject: bar graph color control

Subject: bar graph color control

From: Juliette Salexa

Date: 5 Aug, 2009 21:56:02

Message: 1 of 4

From the help:

"bar(...,'bar_color') displays all bars using the color specified by the single-letter abbreviation 'r', 'g', 'b', 'c', 'm', 'y', 'k', or 'w'. "

What this does is, specifying 'r' makes them ALL red

How about if I wanted specific ones, spefic colors for specific bars:

like:
g=rand(5,1)
colors=['r' 'g' 'g' 'r' 'b']
bar(g,colors)

Is there a way to do this ??

Thanks in advance
xoxo

Subject: bar graph color control

From: ImageAnalyst

Date: 5 Aug, 2009 22:47:03

Message: 2 of 4

On Aug 5, 5:56 pm, "Juliette Salexa" <juliette.physic...@gmail.com>
wrote:
> From the help:
>
> "bar(...,'bar_color') displays all bars using the color specified by the single-letter abbreviation 'r', 'g', 'b', 'c', 'm', 'y', 'k', or 'w'. "
>
> What this does is, specifying 'r' makes them ALL red
>
> How about if I wanted specific ones, spefic colors for specific bars:
>
> like:
> g=rand(5,1)
> colors=['r' 'g' 'g' 'r' 'b']
> bar(g,colors)
>
> Is there a way to do this ??
>
> Thanks in advance
> xoxo

-------------------------------------------------------------------------------------------------------
Here's something I wrote for a 3-color pie chart I was working with.
Perhaps you can adapt it to a bar chart.


%=====================================================================
% If you apply a colormap, MATLAB has a "feature" where it applies the
% colormap to ALL the axes on the figure, not just the current axes.
So if
% you apply a colormap to the current axes (your pie chart) thinking
it
% will affect only your pie chart, you will be surprised to find it
affects
% all other charts and images on the dialog box. To get around that,
use
% this function which the colors of the pie segments and does not
affect
% any other objects in the dialog box. You need to pass in
% hPieComponentHandles which you get when you create the pie chart:
% hPieComponentHandles = pie([Value1, Value2, Value3],{'Label
1','Label 2','Label 3'});
% Then make up your color map like this:
% pieColorMap(1,:) = [.22 .71 .29]; % Color for segment 1.
% pieColorMap(2,:) = [.25 .55 .79]; % Color for segment 2.
% pieColorMap(3,:) = [.93 .11 .14]; % Color for segment 3.
% and finally, call this function
% SetPieChartColors(hPieComponentHandles, pieColorMap);
function SetPieChartColors(hPieComponentHandles, PieSegmentColors)
set(hPieComponentHandles(1),'FaceColor', PieSegmentColors(1,:));
set(hPieComponentHandles(3),'FaceColor', PieSegmentColors(2,:));
set(hPieComponentHandles(5),'FaceColor', PieSegmentColors(3,:));
return;

Subject: bar graph color control

From: Juliette Salexa

Date: 6 Aug, 2009 17:05:20

Message: 3 of 4

Thanks ImageAnalyst!

I was hoping matlab had this feature internally, but I guess I'll have to do with writing my own script.

Your code makes perfect sense to me, except I'm not sure if the same method will work for bar graphs.

The problem is this:

g= pie([5,4,2],{'1','2','3'})
g = 159.0159 160.0056 161.0056 162.0044 163.0043 164.0042

-> there's a value for each segment of the pie, and for each label

h=bar(rand(3,1))
h = 159.0165

-> there's one value that describes the whole thing!

set(h,'FaceColor',[.22 .71 .29])

-> changes ALL bars in h to green!

-------------
I've tried converting rand(3,1) into a 3x1 cell, and using the bargraph command .. but my inexperience lead me to failure!

I really hope I don't have to go and change the bar function itself .. because it looks obfuscated, and it looks like it's written with the intention that all bars would be the same color.

Does anyone know a way around this ?? It must be possible, since the command:
colormap winter

makes the bars different colors.

Subject: bar graph color control

From: Juliette Salexa

Date: 7 Aug, 2009 20:55:03

Message: 4 of 4

I guess something like this is the only way to do it:

bar([5,0,0],'r');
hold on
bar([0,4,0],'b');
bar([0,0,1],'g')

Looks like my code is going to become 100 times as long :(

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
reference Sprinceana 7 Aug, 2009 17:02:24
bar graph color... Sprinceana 7 Aug, 2009 17:02:18
bar Sprinceana 7 Aug, 2009 17:02:18
rssFeed for this Thread

Contact us at files@mathworks.com