COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

Multiple line plots, two y axes

• Follow

```I am trying to plot a total of four lines onto a single line graph. All of the lines are for a common time period, but the y scales vary greatly between the variables.

My variable "AABWannanom" is on the scale of 10^15, but my other three lines "Bannualindex, Mannualindex, Vindexyear" are values between about -5 and +5.

I have found the plotyy function, but that does not allow me to plot more than one line on each axis. Is there anyway to get "plot(time2,Bannualindex,'r',time,Mannualindex,'g',time2,Vindexyear,'b')" onto the secondary y axis?

time=[1958:1:2007];
time2=[1958:1:2005];
figure
plot(time,AABWannanom,'k'),
hold on
plot(time2,Bannualindex,'r',time,Mannualindex,'g',time2,Vindexyear,'b')
hold off

Claire
```
 0

```"Claire " <claire.davis@students.mq.edu.au> wrote in message <i7h93a\$1hj\$1@fred.mathworks.com>...
> I am trying to plot a total of four lines onto a single line graph. All of the lines are for a common time period, but the y scales vary greatly between the variables.
>
> My variable "AABWannanom" is on the scale of 10^15, but my other three lines "Bannualindex, Mannualindex, Vindexyear" are values between about -5 and +5.
>
> I have found the plotyy function, but that does not allow me to plot more than one line on each axis. Is there anyway to get "plot(time2,Bannualindex,'r',time,Mannualindex,'g',time2,Vindexyear,'b')" onto the secondary y axis?
>
> time=[1958:1:2007];
> time2=[1958:1:2005];
> figure
> plot(time,AABWannanom,'k'),
> hold on
> plot(time2,Bannualindex,'r',time,Mannualindex,'g',time2,Vindexyear,'b')
> hold off
>
> Claire

I think, that this is solution of your problem:
================================

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(get(AX(1),'Ylabel'),'String','Slow Decay')
set(get(AX(2),'Ylabel'),'String','Fast Decay')
xlabel('Time (\musec)')
title('Multiple Decay Rates')
set(H1,'LineStyle','--')
set(H2,'LineStyle',':')

axes(AX(1))
hold on
plot(randn(20,1)*100)

axes(AX(2))
hold on
plot(rand(20,1),'y--')
plot(-rand(20,1),'k.')
plot(rand(20,1)-0.5,'ro')
==========================
regards
Grzegorz
```
 0

```
"Claire " <claire.davis@students.mq.edu.au> wrote in message
news:i7h93a\$1hj\$1@fred.mathworks.com...
> I am trying to plot a total of four lines onto a single line graph. All of
> the lines are for a common time period, but the y scales vary greatly
> between the variables.
> My variable "AABWannanom" is on the scale of 10^15, but my other three
> lines "Bannualindex, Mannualindex, Vindexyear" are values between about -5
> and +5.
>
> I have found the plotyy function, but that does not allow me to plot more
> than one line on each axis. Is there anyway to get
> "plot(time2,Bannualindex,'r',time,Mannualindex,'g',time2,Vindexyear,'b')"
> onto the secondary y axis?

Just like PLOT, if you specify a multi-column matrix for the Y input then
PLOTYY will operate on each column.  [It's a _little_ more complicated than
that, so you should probably read through the first two paragraphs of the
Description section of the reference page for PLOT.]

http://www.mathworks.com/help/techdoc/ref/plot.html?BB=1

x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = tan(x);
[ax, h1, h2] = plotyy(x, [y1.' y2.'], x, y3);

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0

```"Steven_Lord" <slord@mathworks.com> wrote in message <i7iaio\$kse\$1@fred.mathworks.com>...
>
>
> "Claire " <claire.davis@students.mq.edu.au> wrote in message
> news:i7h93a\$1hj\$1@fred.mathworks.com...
> > I am trying to plot a total of four lines onto a single line graph. All of
> > the lines are for a common time period, but the y scales vary greatly
> > between the variables.
> > My variable "AABWannanom" is on the scale of 10^15, but my other three
> > lines "Bannualindex, Mannualindex, Vindexyear" are values between about -5
> > and +5.
> >
> > I have found the plotyy function, but that does not allow me to plot more
> > than one line on each axis. Is there anyway to get
> > "plot(time2,Bannualindex,'r',time,Mannualindex,'g',time2,Vindexyear,'b')"
> > onto the secondary y axis?
>
> Just like PLOT, if you specify a multi-column matrix for the Y input then
> PLOTYY will operate on each column.  [It's a _little_ more complicated than
> that, so you should probably read through the first two paragraphs of the
> Description section of the reference page for PLOT.]
>
> http://www.mathworks.com/help/techdoc/ref/plot.html?BB=1
>
> x = 0:0.1:2*pi;
> y1 = sin(x);
> y2 = cos(x);
> y3 = tan(x);
> [ax, h1, h2] = plotyy(x, [y1.' y2.'], x, y3);
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> http://www.mathworks.com

This was an issue I actually gave up on... I should have realized that the reference page to plot should be used for plotyy.... Let alone the 2 paragraphs you point to are so close to the solution you provide, which by the way works for both y axis it appears.  This would be nice to know if you have 3 data sets, two on one y-axis and one on the other, all 3 you would like to fit to some polyfit solution....

Its no wonder Bill Gates is the richest person in America.

This is the first time I have seen a useful answer with respect to plotyy.  Maybe Mathworks should BETTER EXPLAIN the PLOTYY REF PAGE as there seems to be a common theme as most users find this function to be lacking...

At \$2000 + cost of tool boxes, stuff like this make MATLAB a tough sell to a fortune 100 engineering company when Excel seems to do this just fine.

DRIVES ME NUTS AS A MATLAB USER AND EVALUATER...
```
 0
Reply marc.schreier (64) 9/25/2010 6:27:14 AM

```Thanks so much for your help!!! I have managed to plot my four lines on two different axes just like I wanted! I am now trying to tweak the axis properties so that the data is displayed on a tighter axis. I have tried using 'YTick' but it says that this isn't a valid input for the type 'set'.

What I want is to set AX(1) so that the ticks go from [4*10^15:1*10^15:-4*10^15]
and AX(2) goes from [3:1:-3]. What function do I need to use to do this?

Also, the two y axes are being displayed in color. I want them to just be the standard black, but I can't see what function has set them in color in order to reverse it.

Is there somewhere that lists all of the valid properties that I can set to tweak the look of the plot?

time=[1958:1:2007];
time2=[1958:1:2005];
y1=Bannualindex;
y2=Mannualindex(1:(end-2),1);
y3=Vindexyear;
y4=[y1.*y2.*y3]
figure
[AX,L1,L2]=plotyy(time2,(AABWshortannanom'),time2,y4);
set(get(AX(1),'Ylabel'),'String','Volume Anomoly (m^3)')
set(get(AX(2),'Ylabel'),'String','Index Value')
xlabel('Time (years)')
title('AABW volume anomoly vs. SAM Indices')

axes(AX(1))
hold on
plot(time2,(AABWshortannanom'),'k'),

axes(AX(2))
hold on
plot(time2,y1,'r',time2,y2,'g',time2,y3,'b')
```
 0

```"Marc "  wrote
>  Maybe Mathworks should BETTER EXPLAIN the PLOTYY REF PAGE as there
> seems to be a common theme as most users find this function to be lacking...
> At \$2000 + cost of tool boxes, stuff like this make MATLAB a tough sell to a
> fortune 100 engineering company when Excel seems to do this just fine.
> DRIVES ME NUTS AS A MATLAB USER AND EVALUATER...

Marc ... this kind of frustration with plotyy is not uncommon, and in fact I've noticed these same kinds of comments on the newsgroup about plotyy for well over a decade - far more than for most other matlab functions. I think there are two main reasons for this. The first reason is that to get plotyy to do anything but the most obvious plain two trace graph requires one or more auxiliary functions which although well documented are not all documented in the same place. Often users don't even know about most of the auxiliary functions until or unless they have become matlab graphics experts. The 2nd major problem (also related to the documentation) is that there are very few examples of how to use plotyy. Although plot and plotyy are very powerful, and you can eventually use them to graph nearly anything, without many examples showing how its used in various situations it is nearly
impossible for most users to figure out.

Many years ago this sad state of affairs inspired me to attempt to improve this situation with a file exchange submission. To fix the first problem, I put all capability of plot, plotyy, and the numerous auxiliary functions into a single function called "plt". This means all the capabilities are documented in one comprehensive hyper-linked help file. To fix the 2nd problem, I included example code sprinkled thruout the documentation, making it clear how to use each feature. Also included with the main plt.m file are about two dozen example .m files showing how nearly every plt feature can be used. Whenever a plt user has trouble creating the plot they want, I usually clarify the situation by adding an additional example during the next plt update.

plt has been on the file exchange for about 6.5 years now, and judging from the hundreds of emails I've received from users I judge the effort a success. My only disappointment is that such a small percentage of matlab users have tried it. To fix that I need your help. If you do lots of plotting, go out on a limb and download plt. I haven't heard anyone who has regretted giving it a try.

And I have one more suggestion while I'm at it. After installing plt, run demoplt.m which cycles thru all 22 example programs allowing you to quickly see most of plt's functionality. The first email I get from a new user is typically "Wow, this is a nifty plotting application, but I really wish it did ... whatever ..." In most cases, if they had simply run demoplt, they would have quickly seen that in fact plt can do what they were wishing for.

And finally, to really insure the success of this effort, send me an email after you try plt letting me know how it worked out for you and any difficulties you had figuring out how to use it. (My email address is right there at the top of the documentation). As you can see there have been many updates over the years, and nearly every one was inspired by a user comment to two.

And by the way, to find it just search the file exchange for "plt".

~Paul Mennen
```
 0
Reply nospam6526 (263) 10/7/2010 11:49:04 AM

```"Paul Mennen" <nospam@mennen.org> wrote in message <i8kc3g\$mp3\$1@fred.mathworks.com>...
> "Marc "  wrote
> >  Maybe Mathworks should BETTER EXPLAIN the PLOTYY REF PAGE as there
> > seems to be a common theme as most users find this function to be lacking...
> > At \$2000 + cost of tool boxes, stuff like this make MATLAB a tough sell to a
> > fortune 100 engineering company when Excel seems to do this just fine.
> > DRIVES ME NUTS AS A MATLAB USER AND EVALUATER...
>
> Marc ... this kind of frustration with plotyy is not uncommon, and in fact I've noticed these same kinds of comments on the newsgroup about plotyy for well over a decade - far more than for most other matlab functions. I think there are two main reasons for this. The first reason is that to get plotyy to do anything but the most obvious plain two trace graph requires one or more auxiliary functions which although well documented are not all documented in the same place. Often users don't even know about most of the auxiliary functions until or unless they have become matlab graphics experts. The 2nd major problem (also related to the documentation) is that there are very few examples of how to use plotyy. Although plot and plotyy are very powerful, and you can eventually use them to graph nearly anything, without many examples showing how its used in various situations it is nearly
> impossible for most users to figure out.
>
> Many years ago this sad state of affairs inspired me to attempt to improve this situation with a file exchange submission. To fix the first problem, I put all capability of plot, plotyy, and the numerous auxiliary functions into a single function called "plt". This means all the capabilities are documented in one comprehensive hyper-linked help file. To fix the 2nd problem, I included example code sprinkled thruout the documentation, making it clear how to use each feature. Also included with the main plt.m file are about two dozen example .m files showing how nearly every plt feature can be used. Whenever a plt user has trouble creating the plot they want, I usually clarify the situation by adding an additional example during the next plt update.
>
> plt has been on the file exchange for about 6.5 years now, and judging from the hundreds of emails I've received from users I judge the effort a success. My only disappointment is that such a small percentage of matlab users have tried it. To fix that I need your help. If you do lots of plotting, go out on a limb and download plt. I haven't heard anyone who has regretted giving it a try.
>
> And I have one more suggestion while I'm at it. After installing plt, run demoplt.m which cycles thru all 22 example programs allowing you to quickly see most of plt's functionality. The first email I get from a new user is typically "Wow, this is a nifty plotting application, but I really wish it did ... whatever ..." In most cases, if they had simply run demoplt, they would have quickly seen that in fact plt can do what they were wishing for.
>
> And finally, to really insure the success of this effort, send me an email after you try plt letting me know how it worked out for you and any difficulties you had figuring out how to use it. (My email address is right there at the top of the documentation). As you can see there have been many updates over the years, and nearly every one was inspired by a user comment to two.
>
> And by the way, to find it just search the file exchange for "plt".
>
> ~Paul Mennen

Paul

I still do not understand why MATLAB could not add a y-axis (ie. right or left) with plot.  It seems like it would be so easy.

I say again, if MATLAB wants to replace Excel at companies like mine, stuff like this needs to be addressed.  People using Excel for 30 years are not going to switch and its been my mission to get these folks to convert to MATLAB.  If we are an elite, fortune 100 engineering company, one would think we would use the elite tools out there.... Which I know MATLAB is...

I realize I am preaching to the choir here but sometimes I just need to vent...
```
 0

6 Replies
1620 Views

Similiar Articles:

7/20/2012 2:19:44 AM