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:
Plotting two functions on the same plot

Subject: Plotting two functions on the same plot

From: Dinesh Dileep Gaurav

Date: 2 Jul, 2010 17:27:05

Message: 1 of 10

Hi all,
          I want to plot two y-axis data on the same x-axis. The problem is that the first plot should be done using command 'pcolor' and second one using normal 'plot' command.

basically this is what I want to do

command for first plot is

pcolor(x,y1,z);colorbar;


command for second plot is

plot(x,y2)

Here x is x-axis data,y1 and y2 are two different data's against x-axis data.

I want it to happen on the same plot.

The inbuilt matlab command 'plotyy' doesn't help as it uses 'plot' command for both plots. I also tried the matlab help about 'Multiple Axes on Same figure'. But then what happens , the x-axis data which is same for both axes has different scalings on both cases

Subject: Plotting two functions on the same plot

From: Steven Lord

Date: 2 Jul, 2010 17:40:32

Message: 2 of 10


"Dinesh Dileep Gaurav" <raman_dileep@yahoo.co.in> wrote in message
news:i0l7h8$cht$1@fred.mathworks.com...
> Hi all,
> I want to plot two y-axis data on the same x-axis. The problem is
> that the first plot should be done using command 'pcolor' and second one
> using normal 'plot' command.
>
> basically this is what I want to do
>
> command for first plot is
>
> pcolor(x,y1,z);colorbar;
>
>
> command for second plot is
>
> plot(x,y2)
>
> Here x is x-axis data,y1 and y2 are two different data's against x-axis
> data.
>
> I want it to happen on the same plot.
>
> The inbuilt matlab command 'plotyy' doesn't help as it uses 'plot' command
> for both plots.

Double-check the documentation for PLOTYY -- what do the fifth and sixth
input arguments do?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Plotting two functions on the same plot

From: Dinesh Dileep Gaurav

Date: 3 Jul, 2010 05:23:04

Message: 3 of 10

> Double-check the documentation for PLOTYY -- what do the fifth and sixth
> input arguments do?
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>


Thanx for the reply, I checked the documentation for 'plotyy', but it specifies that the functions that can be used are of the form function(x,y). But I want to use 'pcolor' which accepts three arguments.

Subject: Plotting two functions on the same plot

From: Walter Roberson

Date: 4 Jul, 2010 17:38:47

Message: 4 of 10

Dinesh Dileep Gaurav wrote:
>> Double-check the documentation for PLOTYY -- what do the fifth and
>> sixth input arguments do?

> Thanx for the reply, I checked the documentation for 'plotyy', but it
> specifies that the functions that can be used are of the form
> function(x,y). But I want to use 'pcolor' which accepts three arguments.

mypcolor = @(x,y) pcolor(x,y,MyC)

then pass mypcolor as the function

Subject: Plotting two functions on the same plot

From: dinesh dileep gaurav

Date: 2 Aug, 2010 14:23:06

Message: 5 of 10

Walter Roberson <roberson@hushmail.com> wrote in message <HG3Yn.5817$OU6.721@newsfe20.iad>...
> Dinesh Dileep Gaurav wrote:
> >> Double-check the documentation for PLOTYY -- what do the fifth and
> >> sixth input arguments do?
>
> > Thanx for the reply, I checked the documentation for 'plotyy', but it
> > specifies that the functions that can be used are of the form
> > function(x,y). But I want to use 'pcolor' which accepts three arguments.
>
> mypcolor = @(x,y) pcolor(x,y,MyC)
>
> then pass mypcolor as the function

Hello Sir,
                   Sorry for the late reply, I tried the solution you specified recently. I defined the variables as you mentioned. But it results in a error.

??? Error: "mypcolor" was previously used as a variable,
 conflicting with its use here as the name of a function or command.
 See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use Command Syntax" for details.
 

Subject: Plotting two functions on the same plot

From: Andy

Date: 2 Aug, 2010 14:31:09

Message: 6 of 10

Well, he had no way of knowing that you had previously used mypcolor as a variable. Did you try changing the name? For example:

pcolorwrapper = @(x,y) pcolor(x,y,MyC)

Subject: Plotting two functions on the same plot

From: Mirek L

Date: 2 Aug, 2010 14:49:20

Message: 7 of 10

What about the "hold on"
Is it what you want?

%% Test
x = 1:5;
y1 = x*2;
y2 = x.^2/5+2;

pcolor(x,y1,x'*y1)
hold on
plot(x,y2,'r')

Subject: Plotting two functions on the same plot

From: dinesh dileep gaurav

Date: 2 Aug, 2010 15:27:05

Message: 8 of 10

"Andy " <myfakeemailaddress@gmail.com> wrote in message <i36krd$4ss$1@fred.mathworks.com>...
> Well, he had no way of knowing that you had previously used mypcolor as a variable. Did you try changing the name? For example:
>
> pcolorwrapper = @(x,y) pcolor(x,y,MyC)

Hi thanx for the reply. I don't think using mypcolor earlier was the problem because i never used it before other than the line

mypcolor= @(x,y) pcolor(x,y,MyC)
 
My wild guess is Matlab is seeing this as a variable assignment rather than a function.

Subject: Plotting two functions on the same plot

From: dinesh dileep gaurav

Date: 2 Aug, 2010 15:31:20

Message: 9 of 10

"Mirek L" <miroslawl@gmail.com> wrote in message <i36ltg$e6e$1@fred.mathworks.com>...
> What about the "hold on"
> Is it what you want?
>
> %% Test
> x = 1:5;
> y1 = x*2;
> y2 = x.^2/5+2;
>
> pcolor(x,y1,x'*y1)
> hold on
> plot(x,y2,'r')


Hi, thanx for the reply. The problem is I want both the plot's y-axes overlapping, so if y1=1:5

and y2=-1:-1:-5 , if you plot using hold on, it comes on top portion and bottom portion of plot. I want them overlapped, using plotyy will do this, left y-axis will be 1:5 and same on right will be -1:-5. But plotyy (for me) is not working with pcolor.

Subject: Plotting two functions on the same plot

From: Mirek L

Date: 2 Aug, 2010 16:03:06

Message: 10 of 10

"Dinesh Dileep Gaurav" <dineshdileep@gmail.com> wrote in message <i36oc8$s94$1@fred.mathworks.com>...
> "Mirek L" <miroslawl@gmail.com> wrote in message <i36ltg$e6e$1@fred.mathworks.com>...
> > What about the "hold on"
> > Is it what you want?
> >
> > %% Test
> > x = 1:5;
> > y1 = x*2;
> > y2 = x.^2/5+2;
> >
> > pcolor(x,y1,x'*y1)
> > hold on
> > plot(x,y2,'r')
>
>
> Hi, thanx for the reply. The problem is I want both the plot's y-axes overlapping, so if y1=1:5
>
> and y2=-1:-1:-5 , if you plot using hold on, it comes on top portion and bottom portion of plot. I want them overlapped, using plotyy will do this, left y-axis will be 1:5 and same on right will be -1:-5. But plotyy (for me) is not working with pcolor.


'hold on' rulez? ;)


%% Test
 x = 1:5;
 y1 = x;
 y2 = -x;

figure
pcolor(x,y1,x'*y1)
hold on
[AX,H1,H2] = plotyy(x,y1,x,y2)
set(H2,'LineStyle','none')
set(H1,'LineWidth',3)
set(H1,'Color','r')

Tags for 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