Thread Subject: plot second y axis on the fly

Subject: plot second y axis on the fly

From: andy

Date: 16 Sep, 2009 14:28:03

Message: 1 of 3

I would like to plot a second y axis (right side) on the fly and furthermore want multiple data sets referred to for the left axis (or either axis for that matter) and want to be able to have error bars. I bounce among many computers with default installations and would rather avoid plt. plotyy also doesn't seem to have this capacity (at least with reference to having multiple sets on each side, some with error bars and some without), but this seems like such a basic need that I feel like I am missing something. My immediate need is to, in Command Window, do something like:

figure
plot(x1,y1,'bo') %left axis
hold on
errorbar(x2,y2,e2,'ro') %left axis
errobar(x3,y3,e3,'g-') %where this one would be on the RIGHT AXIS

Thank you for your time.

andy

Subject: plot second y axis on the fly

From: arich82

Date: 16 Sep, 2009 16:38:02

Message: 2 of 3

"andy " <ecoandy@yahoo.com> wrote in message <h8qslj$51t$1@fred.mathworks.com>...
> I would like to plot a second y axis (right side) on the fly and furthermore want multiple data sets referred to for the left axis (or either axis for that matter) and want to be able to have error bars. I bounce among many computers with default installations and would rather avoid plt. plotyy also doesn't seem to have this capacity (at least with reference to having multiple sets on each side, some with error bars and some without), but this seems like such a basic need that I feel like I am missing something. My immediate need is to, in Command Window, do something like:
>
> figure
> plot(x1,y1,'bo') %left axis
> hold on
> errorbar(x2,y2,e2,'ro') %left axis
> errobar(x3,y3,e3,'g-') %where this one would be on the RIGHT AXIS
>
> Thank you for your time.
>
> andy

Use the handles returned by plotyy.

I'm not sure why you only have one set of data [x1, y1]; do the differently scaled error bars e2 and e3 both apply to y1, or should there have been a [x4, y4]? Regardless, you could replace NaN's in the following example with [x4, y4] if that's what you meant:

%%
x1 = linspace(0, 2*pi, 101);
y1 = sin(x1);

x2 = x1(1:10:end);
y2 = sin(x2);
e2 = ones(size(x2));

x3 = x1(5:10:end);
y3 = 10*sin(x3);
e3 = 10*ones(size(x3));


hf = igure;
[ha, h1, h2] = plotyy(x1, y1, NaN, NaN)
set(h1, 'Marker', 'o');

hold(ha(1), 'all');
hold(ha(2), 'all');

errorbar(ha(1), x2, y2, e2, 'ro') %left axis
errorbar(ha(2), x3, y3, e3, 'g-') %where this one would be on the RIGHT AXIS

% Let me know if this does what you need, or if I misinterpreted something.

--

Subject: plot second y axis on the fly

From: Paul Mennen

Date: 16 Sep, 2009 16:59:25

Message: 3 of 3

"andy " wrote in message
> plotyy also doesn't seem to have this capacity (at least with
> reference to having multiple sets on each side, some with error
> bars and some without),

Andy, I believe with enough work you can coerce plotyy to do such a thing
although it will be more difficult than it should be. There is an alternative
one the file exchange called plt:

http://www.mathworks.com/matlabcentral/fileexchange/4936

For an example of what you are trying to do (several plots mixing
traces, bars, error bars) try the example "plt\demo\pltvbar.m".

Note that you can put as many of the traces on the right hand axis
as you want - e.g. the parameter 'Right',[2 4 5], will put the 2nd,
4th and the 5th traces on the right axis with the remaining on the left.

If the documentation (plt.chm) doesn't answer all your questions, feel free to ask me:

paul (at) mennen (dot) org

I hope that helps you out.
~Paul

Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com