Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

plotting bars and error bars on same axis using different y-axis

729 views
Skip to first unread message

fdfs tank

unread,
Feb 16, 2012, 2:25:17 AM2/16/12
to
Hello,
I had a question I wanted to ask regarding plotting different types of displays on the same x-axis only using different y-axis. Specifically, I have two data sets which have exactly the same x-axis, so I want to plot them both on the same subplot. The first data I want to plot using an errorbar display and the second one using a bar display. I want to have two different y-axis for each plot. I tried using the plotyy function, but it doesn't work with errorbars and bars displays.
Does anyone have an idea on how to do this?
Thank you :)

dpb

unread,
Feb 16, 2012, 9:30:08 AM2/16/12
to
See documentation in Graphics Chapter on "Using Multiple X and Y Axes"

Also read the plotyy() m-file to see how they do it--it's just two axes
objects to coincide in position w/ properties set() so one has y-axis at
right, other at left. Do the same w/ the two types you want.

If, of course, you do this often wrap it in a function once you get it
worked out...

--

fdfs tank

unread,
Feb 16, 2012, 9:53:35 AM2/16/12
to
Thank you for the reply, but this is not very informative. If someone knows of a way of how I actually do it - the actual code - that would be great!
Thank you :)


dpb <no...@non.net> wrote in message <jhj3ti$rlo$1...@speranza.aioe.org>...

dpb

unread,
Feb 16, 2012, 10:18:55 AM2/16/12
to
On 2/16/2012 8:53 AM, fdfs tank wrote:
> Thank you for the reply, but this is not very informative. If someone
> knows of a way of how I actually do it...

That, of course, _is_ how you actually do it...

--

Steven_Lord

unread,
Feb 16, 2012, 10:19:41 AM2/16/12
to


"fdfs tank" <myar...@walla.co.il> wrote in message
news:jhib0t$9so$1...@newscl01ah.mathworks.com...
> Hello, I had a question I wanted to ask regarding plotting different types
> of displays on the same x-axis only using different y-axis. Specifically,
> I have two data sets which have exactly the same x-axis, so I want to plot
> them both on the same subplot. The first data I want to plot using an
> errorbar display and the second one using a bar display. I want to have
> two different y-axis for each plot. I tried using the plotyy function, but
> it doesn't work with errorbars and bars displays.

Sure it does, you just need to adapt a little bit.

x = 1:10;
y = sin(x);
L = rand(size(x));
U = rand(size(x));
plotyy(x, y, x, y, @bar, @(x, y) errorbar(x, y, L, U));

The anonymous function "adapter" provides PLOTYY the two-input syntax it
expects and ERRORBAR the four inputs it expects.

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

dpb

unread,
Feb 16, 2012, 1:49:12 PM2/16/12
to
On 2/16/2012 9:19 AM, Steven_Lord wrote:
> "fdfs tank" <myar...@walla.co.il> wrote in message
> news:jhib0t$9so$1...@newscl01ah.mathworks.com...
...

>> using the plotyy function, but it doesn't work with errorbars and bars
>> displays.
>
> Sure it does, you just need to adapt a little bit.
...

> plotyy(x, y, x, y, @bar, @(x, y) errorbar(x, y, L, U));
>
> The anonymous function "adapter" provides PLOTYY the two-input syntax it
> expects and ERRORBAR the four inputs it expects.

Ahhhh...another enhancement past my release (actually, several). The
extended function handle syntax and both bar/errorbar postdate R14.

--



Steven_Lord

unread,
Feb 16, 2012, 3:54:27 PM2/16/12
to


"dpb" <no...@non.net> wrote in message news:jhjj39$5re$1...@speranza.aioe.org...
The BAR, ERRORBAR, and PLOTTY functions have been in MATLAB for a very long
time, at least as far back as release R11 (not to be confused with release
R2011a or R2011b.)

>> version
ans =
5.3.0.10183 (R11)

>> help bar

BAR Bar graph.
BAR(X,Y) draws the columns of the M-by-N matrix Y as M groups of N
vertical bars. The vector X must be monotonically increasing or
decreasing.

*snip*

>> help errorbar

ERRORBAR Error bar plot.
ERRORBAR(X,Y,L,U) plots the graph of vector X vs. vector Y with
error bars specified by the vectors L and U. L and U contain the
lower and upper error ranges for each point in Y. Each error bar
is L(i) + U(i) long and is drawn a distance of U(i) above and L(i)
below the points in (X,Y). The vectors X,Y,L and U must all be
the same length. If X,Y,L and U are matrices then each column
produces a separate line.

*snip*

>> help plotyy

PLOTYY Graphs with y tick labels on the left and right.
PLOTYY(X1,Y1,X2,Y2) plots Y1 versus X1 with y-axis labeling
on the left and plots Y2 versus X2 with y-axis labeling on
the right.

*snip*

Function handles were introduced in release R12 and anonymous functions in
release R14, so what I wrote wouldn't work (without some tweaking) in
release R11. But it should work as early as R14.

http://www.mathworks.com/help/techdoc/rn/f8-1009921.html#f8-1011687
0 new messages