Thread Subject: Special Bar Chart

Subject: Special Bar Chart

From: suterr@gmail.com

Date: 7 Sep, 2007 00:55:14

Message: 1 of 8

Hi

I want to plot some kind of bar chart but having problem to use the
bar chart command. I am not sure if there is other more appropriate
command

              Lower Range Upper Range (in KG)
Group A 950 970
Group B 930 980
Group C 960 1000

I want to plot a bar chart that show group A run from 950-970 i.e. bar
with 20 KG that show only from 950 to 970 KG on the bar rather than
two seperate bars that run from 0 to 950 and 970 respectively for
Group A.


Any Ideas?

Thanks

Cheers

Suterr

Subject: Special Bar Chart

From: Lorenzo

Date: 7 Sep, 2007 16:14:05

Message: 2 of 8

 suterr@gmail.com wrote in message
<1189126514.706249.131770@57g2000hsv.googlegroups.com>...
> Hi
>
> I want to plot some kind of bar chart but having problem to use the
> bar chart command. I am not sure if there is other more appropriate
> command
>
> Lower Range Upper Range (in KG)
> Group A 950 970
> Group B 930 980
> Group C 960 1000
>
> I want to plot a bar chart that show group A run from 950-970 i.e. bar
> with 20 KG that show only from 950 to 970 KG on the bar rather than
> two seperate bars that run from 0 to 950 and 970 respectively for
> Group A.
>
>
> Any Ideas?
>
> Thanks
>
> Cheers
>
> Suterr
>

hi Suterr,
two solutions:

% data:
weight = [950 970; 930 980; 960 1000]
range = weight(:,2) - weight(:,1)
weight_diff = [weight(:,1) range]

% solution 1, using bars
figure
bh = bar(weight_diff,'stacked')
set(gca,'ylim',[1 1100])
set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'})
set(bh(1),'FaceColor','none','EdgeColor','none')

% solution 2, using errorbar
figure
eh = errorbar(mean(weight'), range)
set(eh,'linestyle','none')
set(gca,'ylim',[1 1100])
set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'})

hth,
Lorenzo

Subject: Special Bar Chart

From: suterr@gmail.com

Date: 7 Sep, 2007 20:18:55

Message: 3 of 8

Hi Lorenzo,

Thank you so much for the help. Option 1 works almost perfect for my
case.

I do have one additional thing. If Group A also have a second set of
range say 1000 to 1100 Kg and I would like this bar (range 1000-1100)
to fall on the same bar as group A 950-970 i.e. (imagine below is the
resulted bar chart)

Group A 950-970 1000-1100
Group B 930-980
Group C 960-1000

If I used the method you provided I will end up as follows


Group A 950-970
Group A 1000-1100
Group B 930-980
Group C 960-1000


Thanks

BEst,
Suterr

On Sep 7, 9:14 am, "Lorenzo " <lorenz4mat...@gmail.com> wrote:
> sut...@gmail.com wrote in message
>
> <1189126514.706249.131...@57g2000hsv.googlegroups.com>...
>
>
>
> > Hi
>
> > I want to plot some kind of bar chart but having problem to use the
> > bar chart command. I am not sure if there is other more appropriate
> > command
>
> > Lower Range Upper Range (in KG)
> > Group A 950 970
> > Group B 930 980
> > Group C 960 1000
>
> > I want to plot a bar chart that show group A run from 950-970 i.e. bar
> > with 20 KG that show only from 950 to 970 KG on the bar rather than
> > two seperate bars that run from 0 to 950 and 970 respectively for
> > Group A.
>
> > Any Ideas?
>
> > Thanks
>
> > Cheers
>
> >Suterr
>
> hiSuterr,
> two solutions:
>
> % data:
> weight = [950 970; 930 980; 960 1000]
> range = weight(:,2) - weight(:,1)
> weight_diff = [weight(:,1) range]
>
> % solution 1, using bars
> figure
> bh = bar(weight_diff,'stacked')
> set(gca,'ylim',[1 1100])
> set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'})
> set(bh(1),'FaceColor','none','EdgeColor','none')
>
> % solution 2, using errorbar
> figure
> eh = errorbar(mean(weight'), range)
> set(eh,'linestyle','none')
> set(gca,'ylim',[1 1100])
> set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'})
>
> hth,
> Lorenzo

Subject: Special Bar Chart

From: Lorenzo

Date: 7 Sep, 2007 21:42:17

Message: 4 of 8

 suterr@gmail.com wrote in message
<1189196335.742396.282300@r29g2000hsg.googlegroups.com>...
> Hi Lorenzo,
>
> Thank you so much for the help. Option 1 works almost perfect for my
> case.
>
> I do have one additional thing. If Group A also have a second set of
> range say 1000 to 1100 Kg and I would like this bar (range 1000-1100)
> to fall on the same bar as group A 950-970 i.e. (imagine below is the
> resulted bar chart)
>
> Group A 950-970 1000-1100
> Group B 930-980
> Group C 960-1000
>
> If I used the method you provided I will end up as follows
>
>
> Group A 950-970
> Group A 1000-1100
> Group B 930-980
> Group C 960-1000
>
>
> Thanks
>
> BEst,
> Suterr
>

If I understand correctly, you would like to show two colored bars
corresponding to the same group A.
As you may have noticed, what I did with "solution 1" was to plot two bars at
each location, setting the color of the bottom one to "none". To get two
colored bars and nothing in between, you can follow the same strategy, by
plotting four bars, two of which are transparent. This is an example:

weight = [950 970 1000 1100; 930 980 980 980; 960 1000 1000 1000];
range = [weight(:,1), ...
                weight(:,2) - weight(:,1),...
                weight(:,3),...
                weight(:,4) - weight(:,3)];
bh = bar(range,'stacked')
set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'})
set(bh(1),'FaceColor','none')
set(bh(3),'FaceColor','none')
set(bh,'EdgeColor','none')

Play with set to change the colors of the bars.
Lorenzo

Subject: Special Bar Chart

From: suterr@gmail.com

Date: 10 Sep, 2007 07:24:38

Message: 5 of 8

Hi Lorenzo,

Thank you for your help again. The method you provided did manage to
put two bars for the same group. Howewer, for group A one of the bar
is not in the correct range. The range based on the code is 1970-2070
(incorrect range), while the correct range of the bar shall be from
1000-1100.

Best,
Suterr
>
> If I understand correctly, you would like to show two colored bars
> corresponding to the same group A.
> As you may have noticed, what I did with "solution 1" was to plot two bars at
> each location, setting the color of the bottom one to "none". To get two
> colored bars and nothing in between, you can follow the same strategy, by
> plotting four bars, two of which are transparent. This is an example:
>
> weight = [950 970 1000 1100; 930 980 980 980; 960 1000 1000 1000];
> range = [weight(:,1), ...
> weight(:,2) - weight(:,1),...
> weight(:,3),...
> weight(:,4) - weight(:,3)];
> bh = bar(range,'stacked')
> set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'})
> set(bh(1),'FaceColor','none')
> set(bh(3),'FaceColor','none')
> set(bh,'EdgeColor','none')
>
> Play with set to change the colors of the bars.
> Lorenzo

Subject: Special Bar Chart

From: suterr@gmail.com

Date: 10 Sep, 2007 07:52:01

Message: 6 of 8


I think the line 3 in the below code shall be weight(:,3)-weight(:,2).

range = [weight(:,1), ...
          weight(:,2) - weight(:,1),...
          weight(:,3),...
           weight(:,4) - weight(:,3)];

Thanks.

Suterr

On Sep 10, 12:24 am, sut...@gmail.com wrote:
> Hi Lorenzo,
>
> Thank you for your help again. The method you provided did manage to
> put two bars for the same group. Howewer, for group A one of the bar
> is not in the correct range. The range based on the code is 1970-2070
> (incorrect range), while the correct range of the bar shall be from
> 1000-1100.
>
> Best,Suterr
>
>
>
> > If I understand correctly, you would like to show two colored bars
> > corresponding to the same group A.
> > As you may have noticed, what I did with "solution 1" was to plot two bars at
> > each location, setting the color of the bottom one to "none". To get two
> > colored bars and nothing in between, you can follow the same strategy, by
> > plotting four bars, two of which are transparent. This is an example:
>
> > weight = [950 970 1000 1100; 930 980 980 980; 960 1000 1000 1000];
> > range = [weight(:,1), ...
> > weight(:,2) - weight(:,1),...
> > weight(:,3),...
> > weight(:,4) - weight(:,3)];
> > bh = bar(range,'stacked')
> > set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'})
> > set(bh(1),'FaceColor','none')
> > set(bh(3),'FaceColor','none')
> > set(bh,'EdgeColor','none')
>
> > Play with set to change the colors of the bars.
> > Lorenzo

Subject: Special Bar Chart

From: Yogesh

Date: 9 Feb, 2009 20:11:02

Message: 7 of 8

Hello!
This is another solution compared to setting "FaceColor" or "EdgeColor" values to "none".

Method-I: One can just set "visible" property to "off"
Method-II: Delete the unwanted bar graph object. This is useful if you need to use legend to annontate your plot. The demo code is DemoSpecialBar.m
-----------------------------------------------------------
DemoSpecialBar.m
------------------------------------------------------------
%Script File: DemoSpecialBar.m
%Demo script to show parameter range in a bar chart
%Method-1: Use visible property of the object
% will not be useful if one needs to use legend to annotate the graph
%Method-2: Delete the object
% will be useful if one needs to use legend to annotate the graph

clear all;
close all;
clc;

% data:
        %Lower %Higher
        %Bound %Bound
weight = [950 970;
               930 980;
               960 1000];
      
%Compute the range
range = weight(:,2) - weight(:,1);

%Generate matrix with
% %Lower bound %Range
weight_diff = [weight(:,1) range];

figure;

%% Method-1
%Use "visible" property

%Draw bar chart
subplot(1,2,1);
bh = bar(weight_diff,'stacked');

%Set visible property of the object corresponding to lower limit to "off"
set(bh(1),'visible','off');

%set x-axis and y-axis label
xlabel('Groups');
ylabel('Weight (kg)\rightarrow');

%Set x-axis ticklabels
set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'});

%set the range for y-axis
ylim([500 1100]);

%Keep grid visible
grid on;

%There are two object present
%set the legend
legend('Lower bound','Weight');

%% Method-2
%Delete the unwanted object
%Draw bar chart
subplot(1,2,2);
bh = bar(weight_diff,'stacked');

%delete the object corresponding to lower limit
delete(bh(1));

%update the handles for the object
bh = bh(2);

%set x-axis and y-axis label
xlabel('Groups');
ylabel('Weight (kg)\rightarrow');

%Set x-axis ticklabels
set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'});

%set the range for y-axis
ylim([500 1100]);

%Keep grid visible
grid on;

%Only one object present;
legend('Weight');

Subject: Special Bar Chart

From: Yogesh

Date: 9 Feb, 2009 20:11:02

Message: 8 of 8

Hello!
This is another solution compared to setting "FaceColor" or "EdgeColor" values to "none".

Method-I: One can just set "visible" property to "off"
Method-II: Delete the unwanted bar graph object. This is useful if you need to use legend to annontate your plot. The demo code is DemoSpecialBar.m
-----------------------------------------------------------
DemoSpecialBar.m
------------------------------------------------------------
%Script File: DemoSpecialBar.m
%Demo script to show parameter range in a bar chart
%Method-1: Use visible property of the object
% will not be useful if one needs to use legend to annotate the graph
%Method-2: Delete the object
% will be useful if one needs to use legend to annotate the graph

clear all;
close all;
clc;

% data:
        %Lower %Higher
        %Bound %Bound
weight = [950 970;
               930 980;
               960 1000];
      
%Compute the range
range = weight(:,2) - weight(:,1);

%Generate matrix with
% %Lower bound %Range
weight_diff = [weight(:,1) range];

figure;

%% Method-1
%Use "visible" property

%Draw bar chart
subplot(1,2,1);
bh = bar(weight_diff,'stacked');

%Set visible property of the object corresponding to lower limit to "off"
set(bh(1),'visible','off');

%set x-axis and y-axis label
xlabel('Groups');
ylabel('Weight (kg)\rightarrow');

%Set x-axis ticklabels
set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'});

%set the range for y-axis
ylim([500 1100]);

%Keep grid visible
grid on;

%There are two object present
%set the legend
legend('Lower bound','Weight');

%% Method-2
%Delete the unwanted object
%Draw bar chart
subplot(1,2,2);
bh = bar(weight_diff,'stacked');

%delete the object corresponding to lower limit
delete(bh(1));

%update the handles for the object
bh = bh(2);

%set x-axis and y-axis label
xlabel('Groups');
ylabel('Weight (kg)\rightarrow');

%Set x-axis ticklabels
set(gca,'xtick',[1 2 3],'xticklabel',{'A','B','C'});

%set the range for y-axis
ylim([500 1100]);

%Keep grid visible
grid on;

%Only one object present;
legend('Weight');

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
bar chart Yogesh PARTE 9 Feb, 2009 15:15:08
code us 7 Sep, 2007 19:48:25
errorbar Lorenzo 7 Sep, 2007 12:15:21
bar Lorenzo 7 Sep, 2007 12:15:21
bar chart Ned Gulley 6 Sep, 2007 23:28:08
hg Ned Gulley 6 Sep, 2007 23:28:08
graphics Ned Gulley 6 Sep, 2007 23:28:08
rssFeed for this Thread

Contact us at files@mathworks.com