plot two data sets over different time in the same plot
Mostra commenti meno recenti
Hi,
I have two sets of data, say x = 1:10; y = [0,0,3:10] now I want to plot the data points over time t = 1:10 The thing is I do not want to see the 0-value points of y. or put in another way, I want to plot x over selected time t and y over t2. t2 = 3:10 in this case.
If I directly use y(t2) to add on, then the x axis for that plot will starts from 0, instead of 3 as desired!
Thanks, Howie
1 Commento
Howie
il 13 Mag 2013
Risposte (2)
Yao Li
il 13 Mag 2013
plot(t,x)
hold on
plot(t2,y)
14 Commenti
Howie
il 13 Mag 2013
Yao Li
il 13 Mag 2013
I think you'd better give samples of the data points (for example, [1,5,10]) instead of t=1:10.
Howie
il 13 Mag 2013
Yao Li
il 13 Mag 2013
I don't think so. u can add loops for auto-plotting
Howie
il 13 Mag 2013
Yao Li
il 13 Mag 2013
Actually, I'm not quite sure about what you want. For example, t=[t1,t2,t3]; y=[y1,y2,y3]; and each element in t and y is a colunm vector. Make sure the element in t and the corresponding element in y has the same dimention.
for i=1:size(t,2)
hold on
plot(t(:,i),y(:,i))
end
Howie
il 13 Mag 2013
Yao Li
il 13 Mag 2013
The function plot(x,y) plots y vs. x. Thus, no matter what you wanna plot, define the specific x and y.
Yao Li
il 13 Mag 2013
Ah, got it!
for i=1:size(t1,2)
if i>2
figure(1)
hold on
plot(t1(:,i),x(:,i), 'o')
figure(2)
hold on
plot(t1(:,i),y(:,i),'o')
else
figure(1)
hold on
plot(t1(:,i),x(:,i),'o')
end
end
Howie
il 13 Mag 2013
Yao Li
il 13 Mag 2013
Sorry, I think I misunderstood you for a 2nd time. What did you mean when you said connect x and y seperately? Does it mean you wanna plot a line between each point of x and y?
Howie
il 13 Mag 2013
Yao Li
il 14 Mag 2013
connect x to y, or connect a point of x to the next point of x?
Jakob Sørensen
il 13 Mag 2013
Here is an example of how it can be done:
t1 = linspace(0,2*pi,100);
t2 = linspace(pi,2*pi,50);
y1 = sin(t1);
y2 = cos(t2);
plot(t1,y1,t2,y2);
Gives you a plot of a sine (y1) and a cosine (y2), where the cosine only exists from pi:2*pi.
6 Commenti
Howie
il 13 Mag 2013
Jakob Sørensen
il 13 Mag 2013
y1 and y2 are also simply data points. The key thing is the x-axis (t1 and t2). If you try to plot without those, MATLAB won't know when to plot what.
Howie
il 13 Mag 2013
Jakob Sørensen
il 13 Mag 2013
Have a look at your y
>> y = [0 0 3:10];
>> y
y =
0 0 3 4 5 6 7 8 9 10
That means your y vector is 1x10, while your t2-vector (which you use for plotting) is 1x8. So you need to either remove the initial zeros from your y, or make t2 = 1:10.
Yao Li
il 14 Mag 2013
so why not just create a new array which stores only the required points?
Categorie
Scopri di più su 2-D and 3-D Plots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!