Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
bar graph with multiple sub-bars?

Subject: bar graph with multiple sub-bars?

From: J G

Date: 8 Jul, 2012 04:32:05

Message: 1 of 11

Does anyone know of a function, perhaps in the file exchange, to plot a bar graph where each bar has multiple 'sub-bars'?

For example, a bar graph with these matrix data would plot two bars at each of the five points along the x-axis

data1 = [0.4 0.5 0.4 0.9 0.8]
data2 = [0.7 0.5 0.8 0.2 0.5]

However, assume each data point is a mean made up of four numbers. I wondered if there is a nice way to have four (very thin) bars for each main bar?

Thanks.

Subject: bar graph with multiple sub-bars?

From: dpb

Date: 8 Jul, 2012 13:59:59

Message: 2 of 11

On 7/7/2012 11:32 PM, J G wrote:
> Does anyone know of a function, perhaps in the file exchange, to plot a
> bar graph where each bar has multiple 'sub-bars'?
...

> However, assume each data point is a mean made up of four numbers. I
> wondered if there is a nice way to have four (very thin) bars for each
> main bar?
...

What would these individual bars be and how would they actually be
drawn? If they're the observations making up an average, some will be
less, others more than the mean. So, how would you have this look,
precisely? A "ragged" set of bars overlaying the one that represents
the mean, I guess? Just trying to visualize how it would/could be done
if could get an image of the objective clearly...

--

Subject: bar graph with multiple sub-bars?

From: J G

Date: 8 Jul, 2012 21:29:08

Message: 3 of 11

dpb <none@non.net> wrote in message <jtc3p7$a5k$1@speranza.aioe.org>...
> On 7/7/2012 11:32 PM, J G wrote:
> > Does anyone know of a function, perhaps in the file exchange, to plot a
> > bar graph where each bar has multiple 'sub-bars'?
> ...
>
> > However, assume each data point is a mean made up of four numbers. I
> > wondered if there is a nice way to have four (very thin) bars for each
> > main bar?
> ...
>
> What would these individual bars be and how would they actually be
> drawn? If they're the observations making up an average, some will be
> less, others more than the mean. So, how would you have this look,
> precisely? A "ragged" set of bars overlaying the one that represents
> the mean, I guess? Just trying to visualize how it would/could be done
> if could get an image of the objective clearly...
>
> --

Yes, exactly what I was picturing - I realise the raw data bars would sometimes be higher than the main bar, but maybe if they were faded (or more transparent), then the main (bold) bar showing the means would easily be visible...

Subject: bar graph with multiple sub-bars?

From: dpb

Date: 8 Jul, 2012 21:46:32

Message: 4 of 11

On 7/8/2012 4:29 PM, J G wrote:
> dpb <none@non.net> wrote in message <jtc3p7$a5k$1@speranza.aioe.org>...
...
>> ... how would you have this look,
>> precisely? A "ragged" set of bars overlaying the one that represents
>> the mean, I guess? ...
...
> Yes, exactly what I was picturing - I realise the raw data bars would
> sometimes be higher than the main bar, but maybe if they were faded (or
> more transparent), then the main (bold) bar showing the means would
> easily be visible...

Hmmmm....I've done almost nothing w/ bar() in the 30 yrs I've had Matlab
so I'm not at all proficient there but maybe I'll see what happens...

I did play w/ it for another poster wanting to annotate the bars some
not terribly long ago and it seemed to be fairly inflexible as to
spacing, etc. Not sure otomh if there would be a way w/ 'hold' to draw
a second on top of a first that would make multiple groups overlay that
way or not...

Well, actually, it's not as bad as I thought it might be...

y=rand(10,5);
m=mean(y,2);
bar(m), hold on, bar(y)

Try fiddling w/ the color parameters; the bars are not that badly placed...

You may want to save the handles so can muddle around w/ the patches
properties directly, I don't know.

Good luck...

--

Subject: bar graph with multiple sub-bars?

From: dpb

Date: 8 Jul, 2012 22:02:16

Message: 5 of 11

On 7/8/2012 4:46 PM, dpb wrote:
...

> Well, actually, it's not as bad as I thought it might be...
>
> y=rand(10,5);
> m=mean(y,2);
> bar(m), hold on, bar(y)
>
> Try fiddling w/ the color parameters; the bars are not that badly placed...
>
...

At least visible if do

bar(m,'b'), hold on, bar(y,'c') % complementing colors, at least...
axis([0.25 10.75 0 1]) % balance up axes a little...

You can use RGB color triplets as well as one of the eight letter codes
preassigned.

I've not tried to see about coloring the individual bars for each of the
samples within the groups...

--

Subject: bar graph with multiple sub-bars?

From: J G

Date: 8 Jul, 2012 22:27:15

Message: 6 of 11

dpb <none@non.net> wrote in message <jtd01i$fuq$1@speranza.aioe.org>...
> On 7/8/2012 4:46 PM, dpb wrote:
> ...
>
> > Well, actually, it's not as bad as I thought it might be...
> >
> > y=rand(10,5);
> > m=mean(y,2);
> > bar(m), hold on, bar(y)
> >
> > Try fiddling w/ the color parameters; the bars are not that badly placed...
> >
> ...
>
> At least visible if do
>
> bar(m,'b'), hold on, bar(y,'c') % complementing colors, at least...
> axis([0.25 10.75 0 1]) % balance up axes a little...
>
> You can use RGB color triplets as well as one of the eight letter codes
> preassigned.
>
> I've not tried to see about coloring the individual bars for each of the
> samples within the groups...
>
> --Yes, thanks very much - maybe something like this

y=rand(10,5);
m=mean(y,2);
A = bar(m,'w','linewidth',2);
hold on
B = bar(y) ;
set(findobj(B,'Type','patch'),'FaceAlpha',0.1);
set(B(1),'facecolor','c')
set(B(2),'facecolor','b')
set(B(3),'facecolor','g')
set(B(4),'facecolor','r')
set(B(5),'facecolor','y')
axis([0.25 10.75 0 1])

Subject: bar graph with multiple sub-bars?

From: dpb

Date: 8 Jul, 2012 23:17:55

Message: 7 of 11

On 7/8/2012 5:27 PM, J G wrote:
...

>> bar(m,'b'), hold on, bar(y,'c') % complementing colors, at least...
>> axis([0.25 10.75 0 1]) % balance up axes a little...
>>
>> You can use RGB color triplets as well as one of the eight letter
>> codes preassigned.
>>
>> I've not tried to see about coloring the individual bars for each of
>> the samples within the groups...
>>
>> --Yes, thanks very much - maybe something like this
>
> y=rand(10,5);
> m=mean(y,2);
> A = bar(m,'w','linewidth',2);
> hold on
> B = bar(y) ;
> set(findobj(B,'Type','patch'),'FaceAlpha',0.1);
> set(B(1),'facecolor','c')
> set(B(2),'facecolor','b')
> set(B(3),'facecolor','g')
> set(B(4),'facecolor','r')
> set(B(5),'facecolor','y')
> axis([0.25 10.75 0 1])

Not bad...what about adding

set(gca,'color',[0.925 0.925 0.925])

to take some of the sharpness of the blocks off. ???

--

Subject: bar graph with multiple sub-bars?

From: dpb

Date: 8 Jul, 2012 23:41:29

Message: 8 of 11

On 7/8/2012 6:17 PM, dpb wrote:
...

> Not bad...what about adding
>
> set(gca,'color',[0.925 0.925 0.925])
>
> to take some of the sharpness of the blocks off. ???

And, I thought the effect a little better setting the WIDTH parameter
slightly less than the default 0.80 (although that may depend on the
number of bars/elements/average and monitor size, etc., etc., etc., ...

Anyway w/ the default on figure for the sample dataset, I thought 0.75
to give a little extra whitespace between them was easier to view...

Salt to suit, obviously... :)

--

Subject: bar graph with multiple sub-bars?

From: Steven_Lord

Date: 9 Jul, 2012 14:32:47

Message: 9 of 11



"J G" <jemma.geoghegan@gmail.com> wrote in message
news:jtb2g5$g45$1@newscl01ah.mathworks.com...
> Does anyone know of a function, perhaps in the file exchange, to plot a
> bar graph where each bar has multiple 'sub-bars'?
> For example, a bar graph with these matrix data would plot two bars at
> each of the five points along the x-axis
>
> data1 = [0.4 0.5 0.4 0.9 0.8]
> data2 = [0.7 0.5 0.8 0.2 0.5]
>
> However, assume each data point is a mean made up of four numbers. I
> wondered if there is a nice way to have four (very thin) bars for each
> main bar?

Grouped?

 bar(1:2, [1 3 5 7; 2 4 6 8])

Or stacked?

 bar(1:2, [1 3 5 7 ; 2 4 6 8], 'stacked')

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: bar graph with multiple sub-bars?

From: dpb

Date: 9 Jul, 2012 14:59:14

Message: 10 of 11

On 7/9/2012 9:32 AM, Steven_Lord wrote:
> "J G" <jemma.geoghegan@gmail.com> wrote in message
> news:jtb2g5$g45$1@newscl01ah.mathworks.com...
...

>> However, assume each data point is a mean made up of four numbers. I
>> wondered if there is a nice way to have four (very thin) bars for each
>> main bar?
>
> Grouped?
>
> bar(1:2, [1 3 5 7; 2 4 6 8])
>
> Or stacked?
>
> bar(1:2, [1 3 5 7 ; 2 4 6 8], 'stacked')

Did you try the combination of what JG and I worked out, Steven?

It's an extension as neither alternative actually shows the group
statistic (the mean) in addition to the observations for each.

Grouped gets the observations but no mean; stacked ends up w/ the sum
(which for a fixed number of observations/group is proportional but not
the mean). End the end I think his presentation is useful...perhaps an
enhancement request is warranted?

Of course if JG's doing this a lot he'll want to take the combined stuff
and wrap it into a function w/ a nice interface...

--

Subject: bar graph with multiple sub-bars?

From: J G

Date: 9 Jul, 2012 20:49:07

Message: 11 of 11

dpb <none@non.net> wrote in message <jterk9$nlk$1@speranza.aioe.org>...
> On 7/9/2012 9:32 AM, Steven_Lord wrote:
> > "J G" <jemma.geoghegan@gmail.com> wrote in message
> > news:jtb2g5$g45$1@newscl01ah.mathworks.com...
> ...
>
> >> However, assume each data point is a mean made up of four numbers. I
> >> wondered if there is a nice way to have four (very thin) bars for each
> >> main bar?
> >
> > Grouped?
> >
> > bar(1:2, [1 3 5 7; 2 4 6 8])
> >
> > Or stacked?
> >
> > bar(1:2, [1 3 5 7 ; 2 4 6 8], 'stacked')
>
> Did you try the combination of what JG and I worked out, Steven?
>
> It's an extension as neither alternative actually shows the group
> statistic (the mean) in addition to the observations for each.
>
> Grouped gets the observations but no mean; stacked ends up w/ the sum
> (which for a fixed number of observations/group is proportional but not
> the mean). End the end I think his presentation is useful...perhaps an
> enhancement request is warranted?
>
> Of course if JG's doing this a lot he'll want to take the combined stuff
> and wrap it into a function w/ a nice interface...
>
> --Thanks for the replies - yes, I'm happy with what we came up with and I'll work on making it look pretty...

Tags for this Thread

No tags are associated with this thread.

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.

Contact us