Plot two variables as a function of the third one

In summary, you can use the 3d plot feature in Matlab to create a 2D graph that consists of multiple curves, the multiplicity being a function of the third variable.
  • #1
XLR8RPM
5
0
Hello,

I want to plot a curve with variables along the X & Y axis.
But I also want the plot to show multiple curves as a function of another third variable.
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.
I would be doing this Matlab.
So if someone could guide me with the codes.
Hoping for a reply.

Thanks,
Prasad M. Rao.
 
Physics news on Phys.org
  • #2
You will get three dimensional graph since three variables are involved.
you can use 3d plot feature in Matlab to do that
 
  • #3
No I don't want the graph to be 3D, I want it to be 2D but consist of multiple curves, the multiplicity being a function of the third variable for example, 7 curves for 7 days of the week.
 
  • #4
Ok then you can do like this
plot(x,y1,x,y2,x,y3)

Here y1 is array of costs of all fruits on day 1 y2 cost on day 2 etc.,
 
  • #5
Hey n.karthick thanks for replying so quickly.

Thanks for the advice, but here the variables x, y & z are columns of data with more than a thousand readings., how do I work around that ??
Can you post a sample code in Matlab.

Thanks,
Prasad M. Rao.
 
  • #6
Let us assume the relation between 1st variable and 2nd variable in day 1 as
y=2*x^2
and in day 2 as
y=2.5*x^3
You can store 2nd variable in day1 in the variable y1 and day2 in y2
so y1=2*x (day1) and y2=2.5*x (day2) and so on.
Now you can plot 2D curve for all days like this
plot(x,y1,x,y2...)

As you mentioned if number of days are many you can do like this

x=[2 3 ]; % enter values of x
n=2;
m=2;
for i=1:n % n=no of days
for j=1:m %length of x
if i==1 % day 1 relation
y(i,j)=2*x(j)^2;
end
if i==2 % day 2 relation
y(i,j)=2.5*x(j)^3;
end
end
plot(x,y(i,:))
hold on % This will hold the current figure and plot the next values in same figure
end
 
  • #7
Hey n.karthick, once again thanks for replying.
The relation that you suggested is very very difficult in my case, since I'm dealing with raw data and curve fitting is very difficult in my case and I will lose the essence of the data entirely.
Somebody elsewhere suggested the 'unique' and 'sort' functions.
Can you help me out with those codes.
I'm kinda new to Matlab codes :(

Thanks,
Prasad M. Rao.
 
  • #8
What I meant is you can store independent variable as a column vector say x and other two variables as a two dimensional array say y, in which first column of y represents second variable and second column contains third variable. So you can plot II and III variable as a function of I variable. (This way there is no need for curve fitting). Is there any difficulty in this method? If you can explain your problem in detail you may get answer
 
  • #9
Hey n.kartick 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!
 

What is the purpose of plotting two variables as a function of a third one?

The purpose of plotting two variables as a function of a third one is to visualize the relationship between the two variables in relation to the third variable. It allows for a better understanding of how the two variables are affected by changes in the third variable.

What type of graph is typically used for plotting two variables as a function of a third one?

A scatter plot is typically used for plotting two variables as a function of a third one. This type of graph displays the relationship between the two variables in a visual and intuitive way, allowing for patterns and trends to be easily identified.

How do I choose which variable to plot on the x-axis and which variable to plot on the y-axis?

The choice of which variable to plot on the x-axis and which variable to plot on the y-axis depends on the research question or hypothesis being tested. The independent variable, or the variable that is manipulated, is typically plotted on the x-axis, while the dependent variable, or the variable that is measured, is plotted on the y-axis.

What are some common mistakes to avoid when plotting two variables as a function of a third one?

Some common mistakes to avoid when plotting two variables as a function of a third one include using the wrong type of graph, not properly labeling the axes, and not providing a clear and concise title for the graph. It is also important to ensure that the data is accurately represented and that any outliers or anomalies are appropriately addressed.

What other types of graphs can be used to plot two variables as a function of a third one?

In addition to scatter plots, other types of graphs that can be used to plot two variables as a function of a third one include line graphs, bar graphs, and box plots. The choice of which type of graph to use depends on the type of data and the research question being investigated.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
839
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
695
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • General Discussion
Replies
2
Views
386
Back
Top