Got Questions? Get Answers.
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 a graph of three variables

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 29 Aug, 2010 09:00:07

Message: 1 of 25

Hello,

I would like to plot a graph of three variables,two of which would be plotted along the X & Y axis, while the third would direct the curves i.e. the third variable would plot multiple curves as functions of the variables present in X & Y.
Hoping for a reply.

Thanks,
Prasad M. Rao.

Subject: Plotting a graph of three variables

From: Ross W

Date: 29 Aug, 2010 10:44:03

Message: 2 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i5d7in$p17$1@fred.mathworks.com>...
> Hello,
>
> I would like to plot a graph of three variables,two of which would be plotted along the X & Y axis, while the third would direct the curves i.e. the third variable would plot multiple curves as functions of the variables present in X & Y.
> Hoping for a reply.
>
> Thanks,
> Prasad M. Rao.

Hi

It's not very clear what you want.
Have you looked at the contour command?

Ross

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 29 Aug, 2010 12:38:03

Message: 3 of 25

For example, if I'm plotting a curve of fruits vs. cost, I would get one curve.
But how to plot it for say, seven days of the week, thus showing seven curves in the same graph.
So if you could guide me with the codes it would be great.

Thanks,
Prasad M. Rao.

Subject: Plotting a graph of three variables

From: David Young

Date: 29 Aug, 2010 12:45:05

Message: 4 of 25

Plot the first curve, then give the command "hold on" and plot the remaining curves. They will appear on the same axes. You can control the colours etc if you want to distinguish them.

It's also possible to do it by putting all the data into matrices and making one call to the plot function, but you might find the first way easiest.

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 29 Aug, 2010 15:14:03

Message: 5 of 25

No I've already tried the hold function, it tries to fit the data as two separate curves.
What I want is the third variable to show up as different curve altogether.
Just to show the trend that exists because of introduction of the third variable.

Thanks,
Prasad M. Rao.

Subject: Plotting a graph of three variables

From: Roger Stafford

Date: 29 Aug, 2010 19:45:04

Message: 6 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i5dtfr$10r$1@fred.mathworks.com>...
> No I've already tried the hold function, it tries to fit the data as two separate curves.
> What I want is the third variable to show up as different curve altogether.
> Just to show the trend that exists because of introduction of the third variable.
>
> Thanks,
> Prasad M. Rao.
- - - - - - - - - -
  To do what you apparently want, it would be necessary to sort out the variables into separate groups in accordance with your third variable's values and to do a separate plot for each group using the hold function to combine them in a single figure.

  This could be done using the 'unique' and 'sort' functions as follows. Let row vectors of the three variables be called U, V, W (all of the same length.)

 [t,m,n] = unique(W); % Find the unique third parameter values
 [t,p] = sort(n); % Put indices into separate groups in p
 q = 1:length(p);
 q(p) = q; % Get the inverse of p
 r = [0,q(m)]; % Get indices of last members of each group
 hold on
 for k = 1:length(r)-1
  s = p(r(k)+1:r(k+1)); % Get indices for entire k-th group
  plot(U(s),V(s),'y-') % Plot that group
 end
 hold off

  As David suggests, you can distinguish between the separate curves by using a 'linespec' quantity indexed for color by the index k. The above only separates them by not joining the different groups with connecting yellow lines.

Roger Stafford

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 30 Aug, 2010 15:52:06

Message: 7 of 25

Thanks Roger for replying.
I think I'm getting there.

But your code gave the following error:
??? Error using ==> plot
Error in color/linetype argument

Also I found a graph on the internet very similar to the one I want.
Its the image with the name Frame 2.
Here's the link:
http://www.ncl.ucar.edu/Document/Manuals/Getting_Started/Examples/gsun08n.shtml

Hope you reply soon, I feel I'm getting closer.

Thanks,
Prasad M. Rao.

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 18 Sep, 2010 13:46:03

Message: 8 of 25

Hey, I really need your help.
Apologies for the delay.
Lets see how helpful I cane be in my problem.
I have three variables say X, Y and Z.
I need to plot X vs. Y.
Now the problem:
1. Y is changing, so is Z.
So now what I need is the graph of X vs. Y at different Z.
2. Now all the variables (X,Y & Z) have thousands of values in them, but I would like the graphs at just some discrete points of Z.
3. OK I know that the graph of X vs. Y is a curve. So now all I want is multiple graphs on the same page of X vs. Y at different Z.
The people who know the exact code, said that I needed to use one or two for loops, the plot and the hold commands.
Please need help on this.
Thanks!

Subject: Plotting a graph of three variables

From: Ross W

Date: 18 Sep, 2010 14:39:04

Message: 9 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i72fqr$gip$1@fred.mathworks.com>...
> Hey, I really need your help.
> Apologies for the delay.
> Lets see how helpful I cane be in my problem.
> I have three variables say X, Y and Z.
> I need to plot X vs. Y.
> Now the problem:
> 1. Y is changing, so is Z.
> So now what I need is the graph of X vs. Y at different Z.
> 2. Now all the variables (X,Y & Z) have thousands of values in them, but I would like the graphs at just some discrete points of Z.
> 3. OK I know that the graph of X vs. Y is a curve. So now all I want is multiple graphs on the same page of X vs. Y at different Z.
> The people who know the exact code, said that I needed to use one or two for loops, the plot and the hold commands.
> Please need help on this.
> Thanks!

%1000 X Y Z triplets
X=rand(1000,1);
Y=rand(1000,1);
Z=X.*Y;

%gridded z values byinterpolation
xi=linspace(min(X),max(X),100);
yi=linspace(min(Y),max(Y),100);
z=griddata(xi,yi,X,Y,Z);

%plot x Y locations with equal Z values

Ross
contour(xi,yi,z,10)

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 19 Sep, 2010 00:16:05

Message: 10 of 25

Hey Ross, thanks for replying,

I'm getting this error code, can this be solved.

??? Error using ==> griddata at 53
X and Y must be same length as Z or the lengths
of X and Y must match the size of Z.

I guess since this is accumulated data, the lengths and types of each of the variables is not matching.
Can you help me out here.
Thanks !

Subject: Plotting a graph of three variables

From: Ross W

Date: 19 Sep, 2010 00:40:12

Message: 11 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i73ko5$mv0$1@fred.mathworks.com>...
> Hey Ross, thanks for replying,
>
> I'm getting this error code, can this be solved.
>
> ??? Error using ==> griddata at 53
> X and Y must be same length as Z or the lengths
> of X and Y must match the size of Z.
>
> I guess since this is accumulated data, the lengths and types of each of the variables is not matching.
> Can you help me out here.
> Thanks !

It failed because I wrote code without checking it. My mistake, sorry

it was all ok except the griddata call, which should have been

z=griddata(X,Y,Z,xi,yi');

Ross

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 19 Sep, 2010 01:14:04

Message: 12 of 25

Hey Ross, once again thanks!

It worked at least the code did marvellously.
But I didn't get the curves as seen when only it was X vs. Y.
I think that at least the behaviour of the curve shouldn't have changed.

What I would like to ask tell is this:
When I said thousands of values I meant both positive and negative.
The values in X are in the range from -20 to +20.
So also is the range of Y from -10,000 to +10000.
Also Z has values from -15,000 to +15,000.

I hope you understand and can help me out here.
Z cannot be generated it is already present.

Also I found a graph on the internet very similar to the one I want.
Its the image with the name Frame 2.
Here's the link:
http://www.ncl.ucar.edu/Document/Manuals/Getting_Started/Examples/gsun08n.shtml
Please help! :(

Subject: Plotting a graph of three variables

From: Ross W

Date: 19 Sep, 2010 07:20:23

Message: 13 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i73o4s$nmb$1@fred.mathworks.com>...
> Hey Ross, once again thanks!
>
> It worked at least the code did marvellously.
> But I didn't get the curves as seen when only it was X vs. Y.
> I think that at least the behaviour of the curve shouldn't have changed.
>
> What I would like to ask tell is this:
> When I said thousands of values I meant both positive and negative.
> The values in X are in the range from -20 to +20.
> So also is the range of Y from -10,000 to +10000.
> Also Z has values from -15,000 to +15,000.
>
> I hope you understand and can help me out here.
> Z cannot be generated it is already present.
>
> Also I found a graph on the internet very similar to the one I want.
> Its the image with the name Frame 2.
> Here's the link:
> http://www.ncl.ucar.edu/Document/Manuals/Getting_Started/Examples/gsun08n.shtml
> Please help! :(

You said in of your comments that you want to plot X-Y curves for a few particualr values of Z.

So how about you make a list of those Zvalues.

Zlist=[2 3 4];

and you also have your X, Y, Z

for m=1:numel(Zlist)
 n=find(Z==Zlist(m));
 plot(X(n),Y(n))
end

This is a lot like what Roger suggested earlier (though his code is more efficient), but perhaps simpler for you to debug.

Ross

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 19 Sep, 2010 13:00:06

Message: 14 of 25

Hey Ross, thanks once again!

I tried the code but it ain't workin' but Roger's ran, but its not conclusive.
I beg you to please see the image, maybe it will help.
The curves w.r.t. X and Y are plotted, but there are multiple ones because Z is changing too.
Roger mentions something about linespec, can you enlighten me on that.
These graphs are so common now-a-days but so hard to get them plotted.
Waiting in anticipation,
Thanks.

Subject: Plotting a graph of three variables

From: Ross W

Date: 19 Sep, 2010 21:19:04

Message: 15 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i751gm$b82$1@fred.mathworks.com>...
> Hey Ross, thanks once again!
>
> I tried the code but it ain't workin' but Roger's ran, but its not conclusive.
> I beg you to please see the image, maybe it will help.
> The curves w.r.t. X and Y are plotted, but there are multiple ones because Z is changing too.
> Roger mentions something about linespec, can you enlighten me on that.
> These graphs are so common now-a-days but so hard to get them plotted.
> Waiting in anticipation,
> Thanks.

I saw an image showing 4 or 5 colored lines. Each line was drawn on an x-y plot, and corresponded to a specific value of another variable.
This code makes lines of different colors on an x-y plot

%X Y Z are vectors of equal length.
Zlist=[2 3 4]; %sample values of interest - change to suit
linecol='rgbymc';
for m=1:numel(Zlist)
   n=find(Z==Zlist(m));
   plot(X(n),Y(n),linecol(m))
   hold on
end

you can read about linespec here: http://www.mathworks.com/help/techdoc/ref/linespec.html this is the first hit that comes up when i google 'matlab plot linespec'

Ross

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 1 Oct, 2010 15:40:05

Message: 16 of 25

Hey Ross I hope its not too late.

I've finally got the logic required to plot my graphs.
I hope you help me out now.
What I'm dealing with is raw data with multiple channels, but the best part is that these channels are all recorded at the same time that means the value for X at say reading no. 12990 corresponds exactly to reading no. 12990 for Y and similarly for Z too.
So what I need to do is to...
FIRST, filter out data corresponding to Z equal to the value required say maybe 1.25, so since the data is continuous I filter out data for the range of Z between 1.2 & 1.3.
So now I have the data for X & Y for this Z in the range (1.2, 1.3).
SECOND, now I just plot the X & Y at a different colour for Z=1.25.
THIRD, using the hold command I do so for as many Z values that I want thus filtering out the data as I go for each individual graph.
I'm not too good with the program syntax but I'm guessing I need the for loop and the hold command along with some other commands.
Hope you help me out with the codes, I'm sure this is it.
Please, fingers crossed here.
Thanks.

Subject: Plotting a graph of three variables

From: Marc

Date: 1 Oct, 2010 22:16:20

Message: 17 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i84vcl$c05$1@fred.mathworks.com>...
> Hey Ross I hope its not too late.
>
> I've finally got the logic required to plot my graphs.
> I hope you help me out now.
> What I'm dealing with is raw data with multiple channels, but the best part is that these channels are all recorded at the same time that means the value for X at say reading no. 12990 corresponds exactly to reading no. 12990 for Y and similarly for Z too.
> So what I need to do is to...
> FIRST, filter out data corresponding to Z equal to the value required say maybe 1.25, so since the data is continuous I filter out data for the range of Z between 1.2 & 1.3.
> So now I have the data for X & Y for this Z in the range (1.2, 1.3).
> SECOND, now I just plot the X & Y at a different colour for Z=1.25.
> THIRD, using the hold command I do so for as many Z values that I want thus filtering out the data as I go for each individual graph.
> I'm not too good with the program syntax but I'm guessing I need the for loop and the hold command along with some other commands.
> Hope you help me out with the codes, I'm sure this is it.
> Please, fingers crossed here.
> Thanks.

At this point, you should paste the code you have as the other posters have solved this issue.

It sounds like you will can use 'find' to get your values for Z. Something like...

IX = find (Z > 1.2 & Z < 1.3) % this will give you the indices where Z is between
 % those values
Then a simple call as such
X1 = X(IX);
Y1 = Y(IX);
gives you vectors at that corresponding Z...
Lets say you have 3 Z values, then you should get X1, X2, X3 and Y1, Y2, Y3...

figure(1)
plot(X1, Y1,'-r')
hold on
plot(X2, Y2,'-b')
hold on
plot(X3, Y3,'-g')
hold off

If you know that all of these are going to be of equal length then you could save these into matrices and use "for" loops to do this.

Something like

figure(1)
for i = 1:3
  plot(X(:,i),Y(:,i),'-')
  hold on
end
hold off

doc find
doc plot

Subject: Plotting a graph of three variables

From: Ross W

Date: 1 Oct, 2010 22:57:04

Message: 18 of 25

"Marc " <marc.schreier@uop.com> wrote in message <i85mjk$eia$1@fred.mathworks.com>...
> "Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i84vcl$c05$1@fred.mathworks.com>...
> > Hey Ross I hope its not too late.
> >
> > I've finally got the logic required to plot my graphs.
> > I hope you help me out now.
> > What I'm dealing with is raw data with multiple channels, but the best part is that these channels are all recorded at the same time that means the value for X at say reading no. 12990 corresponds exactly to reading no. 12990 for Y and similarly for Z too.
> > So what I need to do is to...
> > FIRST, filter out data corresponding to Z equal to the value required say maybe 1.25, so since the data is continuous I filter out data for the range of Z between 1.2 & 1.3.
> > So now I have the data for X & Y for this Z in the range (1.2, 1.3).
> > SECOND, now I just plot the X & Y at a different colour for Z=1.25.
> > THIRD, using the hold command I do so for as many Z values that I want thus filtering out the data as I go for each individual graph.
> > I'm not too good with the program syntax but I'm guessing I need the for loop and the hold command along with some other commands.
> > Hope you help me out with the codes, I'm sure this is it.
> > Please, fingers crossed here.
> > Thanks.

A variation on my previous post ...

Zlist=[1.2 1.3 1.4 ];

and you also have your X, Y, Z

col='rgbymck'; %a list of colours (longer than it needs to be ...)

for m=1:numel(Zlist)-1
 n=find(Z>Zlist(m) & Z<=Zlist(m+1));
 plot(X(n),Y(n),col(m))
 hold on
end

Ross

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 2 Oct, 2010 04:10:21

Message: 19 of 25

Hey Mark your code worked great, but just one tiny problem, however I try, there are 3 separate plots on 3 graphs rather than 3 plots on the same graph.
I don't know why the hold function ain't working.
Please advise.

Hey Ross, thanks for all the help, your code gave this error
??? Error using ==> and
Too many input arguments.

Subject: Plotting a graph of three variables

From: Ross W

Date: 2 Oct, 2010 07:12:04

Message: 20 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i86bbc$sqi$1@fred.mathworks.com>...
> Hey Mark your code worked great, but just one tiny problem, however I try, there are 3 separate plots on 3 graphs rather than 3 plots on the same graph.
> I don't know why the hold function ain't working.
> Please advise.
>
> Hey Ross, thanks for all the help, your code gave this error
> ??? Error using ==> and
> Too many input arguments.

hmmmm ... maybe some missing parentheses in my for loop? I don't have matlab with me now, so perhaps you can try to sort it out if it fails again?

Zlist=[1.2 1.3 1.4 ];

col='rgbymck'; %a list of colours (longer than it needs to be ...)

for m=1:(numel(Zlist)-1)
 n=find(Z>Zlist(m) & Z<=Zlist(m+1));
 plot(X(n),Y(n),col(m))
 hold on
end

Ross

Subject: Plotting a graph of three variables

From: Marc

Date: 3 Oct, 2010 04:32:03

Message: 21 of 25

"Prasad M. Rao" <XLR8RPM@Gmail.com> wrote in message <i86bbc$sqi$1@fred.mathworks.com>...
> Hey Mark your code worked great, but just one tiny problem, however I try, there are 3 separate plots on 3 graphs rather than 3 plots on the same graph.
> I don't know why the hold function ain't working.
> Please advise.
>
> Hey Ross, thanks for all the help, your code gave this error
> ??? Error using ==> and
> Too many input arguments.

As I said, POST YOUR CODE....

I get paid $80 per hour and at this point without your code I could care less.

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 3 Oct, 2010 06:36:03

Message: 22 of 25

@ Mark: Here's the code, it's just your code.

IX1 = find (LS > 0 & LS < 1.0)
X1 = SM(IX1);
Y1 = FB(IX1);
figure(1)
plot(X1, Y1,'-r')
hold on

IX2 = find (LS > 1 & LS < 2.0)
X2 = SM(IX2);
Y2 = FB(IX2);
figure(2)
plot(X2, Y2,'-b')
hold on

IX3 = find (LS > 2 & LS < 3.0)
X3 = SM(IX3);
Y3 = FB(IX3);
figure(3)
plot(X3, Y3,'-g')
hold off

Hoping for a reply.
Thanks.

Subject: Plotting a graph of three variables

From: dpb

Date: 3 Oct, 2010 13:48:59

Message: 23 of 25

Prasad M. Rao wrote:
> @ Mark: Here's the code, it's just your code.
>
> IX1 = find (LS > 0 & LS < 1.0) X1 = SM(IX1);
> Y1 = FB(IX1);
> figure(1)
...
> figure(2)
> plot(X2, Y2,'-b')
> hold on
...
> figure(3)
> plot(X3, Y3,'-g')
> hold off
>
> Hoping for a reply.
> Thanks.

Well, you could have done last time and at least one would think you
could spell Marc's name correctly.

If you're going to specify a unique figure number before each plot(), of
course you're going to get a new figure...don't do that.

--

Subject: Plotting a graph of three variables

From: Prasad M. Rao

Date: 26 Oct, 2010 13:07:03

Message: 24 of 25

Thank-you very much for all the help Marc & dpb.
But I guess my last question could be how to filter data for a specific value rather than the range specified in the above codes.
But a hearty thanks to all none the less.
Thanks.

Subject: Plotting a graph of three variables

From: dpb

Date: 26 Oct, 2010 13:17:02

Message: 25 of 25

Prasad M. Rao wrote:
...
> But I guess my last question could be how to filter data for a specific
> value rather than the range specified in the above codes.
...

Write the condition you're interested...find() works w/ "==" as well as
inequalities or you can use logical addressing.

Nota Bene however--when you ask for exact equality of floating point
values, that's what you get. You'll probably want to build in some
tolerance or the results may be your follow-up question.

Search the archives and the TMW Matlab wiki; it's a FAQ...

--

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