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 chart with error bars

Subject: bar chart with error bars

From: Kevin Murphy

Date: 8 May, 2000 18:39:14

Message: 1 of 9

Does anyone know how to add error bars to a bar chart?

Kevin

Subject: bar chart with error bars

From: John Williams

Date: 9 May, 2000 12:04:40

Message: 2 of 9

Kevin Murphy wrote:
>
> Does anyone know how to add error bars to a bar chart?

with the ERRORBAR command perhaps?

or are you looking for something different to what that provides?

Still, it should give you a starting point.

John

Subject: bar chart with error bars

From: Kevin Murphy

Date: 8 May, 2000 19:59:14

Message: 3 of 9

I call 'bar' on a matrix, so it gives me clusters of vertical bars like
this

       *
* *
** *
** **

I want to superimpose vertical error bars on these...

Subject: bar chart with error bars

From: John Williams

Date: 9 May, 2000 16:14:08

Message: 4 of 9

Kevin Murphy wrote:
>
> I call 'bar' on a matrix, so it gives me clusters of vertical bars like
> this
>
> *
> * *
> ** *
> ** **
>
> I want to superimpose vertical error bars on these...


You already know the upper/lower error bounds, and just want to draw
them on the graph? I don't know the exact answer off the top of my
head, but I think you would get some good ideas by looking at the file
errorbar.m

Have you searched the user-contributed files and "solution search" at
www.mathworks.com? That's also a good source for things which you think
"surely somebody has wanted to do this before"!

John

Subject: bar chart with error bars

From: Michael Robbins

Date: 9 May, 2000 05:25:03

Message: 5 of 9

I'm not sure I understand the question. Why not use BAR, then
HOLD ON, then ERRORBAR?

Michael Robbins, CFA
Director, Proprietary Trading
Debt Capital Markets
Canadian Imperial Bank of Commerce, World Markets
* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

Subject: bar chart with error bars

From: Kevin Murphy

Date: 9 May, 2000 10:20:12

Message: 6 of 9

Indeed, Solution Number: 25476 specifies how to add error bars to a bar
chart:

y=rand(3,1);
e=std(y)*ones(size(y));
bar(y)
hold on
h=errorbar(y,e);
set(h(1),'color','r')
set(h(2),'LineStyle','none')
hold off

The trouble is that, if y is a matrix, errorbar always places the error
bars in the center of the cluster of bars. e.g.,

mu=rand(2,3);
sigma=randn(2,3);
h = bar(mu);
hold on
errorbar(mu, sigma, '.')
hold off

produces something like this
  
 _
 | *_
*| *|
*** ***
*** ***
 | |

with all the bars superimposed in each cluster.

In principle I can use the patch handles returned by bar, but I don't
understand how to do this.


Kevin

Subject: bar chart with error bars

From: Christopher R. Di Fronzo

Date: 9 May, 2000 15:51:33

Message: 7 of 9

Hi Kevin,

You can have the ERRORBAR function return the handle to the line (and
point) that is created. Then you can move the line (and point) by
changing the objects "XData" property.

For example:

% Your code
mu=rand(2,3);
sigma=randn(2,3);
h = bar(mu);
hold on

% Create first error bar and get handle
eb1 = errorbar(mu(1), sigma(1), '.');

% Move line and point
set(eb1(1), 'xdata', ones(1,9)*.95);
set(eb1(2), 'xdata', .95);

% Create second error bar and get handle
eb2 = errorbar(mu(2), sigma(2), '.');

% Create third error bar and get handle
eb3= errorbar(mu(3), sigma(3), '.');

% Move line and point
set(eb3(1), 'xdata', ones(1,9)*1.05);
set(eb3(2), 'xdata', 1.05);


NOTE: I have done each step explicitly. You can combine some of the
above steps or put them in a loop if you choose.

I hope this helps,

Christopher R. Di Fronzo
difronzo@mathworks.com

Kevin Murphy wrote:
>
> Indeed, Solution Number: 25476 specifies how to add error bars to a bar
> chart:
>
> y=rand(3,1);
> e=std(y)*ones(size(y));
> bar(y)
> hold on
> h=errorbar(y,e);
> set(h(1),'color','r')
> set(h(2),'LineStyle','none')
> hold off
>
> The trouble is that, if y is a matrix, errorbar always places the error
> bars in the center of the cluster of bars. e.g.,
>
> mu=rand(2,3);
> sigma=randn(2,3);
> h = bar(mu);
> hold on
> errorbar(mu, sigma, '.')
> hold off
>
> produces something like this
>
> _
> | *_
> *| *|
> *** ***
> *** ***
> | |
>
> with all the bars superimposed in each cluster.
>
> In principle I can use the patch handles returned by bar, but I don't
> understand how to do this.
>
> Kevin

Subject: bar chart with error bars

From: ykondo@eco.toyama-u.ac.jp (Yasushi KONDO)

Date: 10 May, 2000 03:56:09

Message: 8 of 9

Hi Kevin,

> The trouble is that, if y is a matrix, errorbar always places the error
> bars in the center of the cluster of bars. e.g.,
>
> mu=rand(2,3);
> sigma=randn(2,3);
> h = bar(mu);
> hold on
> errorbar(mu, sigma, '.')
> hold off

The main reason of your trouble is that you do not supply X-data
for the command ERRORBAR;

M=2; N=3; mu=rand(M,N); sigma=randn(M,N);
h=bar(mu); hold on; errorbar(mu,sigma,'.'); hold off;

The statement errorbar(mu,sigma,'.') is equivalent to
errorbar(repmat((1:M)',1,N),mu,sigma,'.').
So, with an appropriate matrix, say x, you can draw what you want
by errorbar(x,mu,sigma,'.').

Now, let me explain how to make the matrix x.
By hx=get(h,'XData'), hx is an N-by-1 cell array; hx{i} is a 4-by-M
matrix such that hx{i}(1,m), hx{i}(2,m), hx{i}(3,m), and hx{i}(4,m)
are, respectively, x-value of bottom-left, top-left, top-right,
and bottom-right vertex of i-th box in m-th block.
  %% That is, both isequal(hx{i}(1,:),hx{i}(2,:)) and
  %% isequal(hx{i}(3,:),hx{i}(4,:)) are TRUE.
To place each error bar at the top-center of the corresponding box,
the mean of the 2nd and 3rd rows of [hx{:}] is helpful.

Then, given mu and sigma (they must be of the same size,
as you know), try
 [M,N]=size(mu);
 h=bar(mu); hold on;
 hx=get(h,'XData'); x=[hx{:}]; x=reshape(mean(x(2:3,:)),M,N);
 errorbar(x,mu,sigma,'.');
 hold off;


Hope this helps!

Subject: bar chart with error bars

From: Kevin Murphy

Date: 10 May, 2000 12:14:34

Message: 9 of 9

Thank you very much, it works a charm.

Kevin

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