plot two data sets over different time in the same plot
显示 更早的评论
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
回答(2 个)
Yao Li
2013-5-13
plot(t,x)
hold on
plot(t2,y)
14 个评论
Howie
2013-5-13
Yao Li
2013-5-13
I think you'd better give samples of the data points (for example, [1,5,10]) instead of t=1:10.
Howie
2013-5-13
Yao Li
2013-5-13
I don't think so. u can add loops for auto-plotting
Howie
2013-5-13
Yao Li
2013-5-13
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
2013-5-13
Yao Li
2013-5-13
The function plot(x,y) plots y vs. x. Thus, no matter what you wanna plot, define the specific x and y.
Yao Li
2013-5-13
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
2013-5-13
Yao Li
2013-5-13
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
2013-5-13
Yao Li
2013-5-14
connect x to y, or connect a point of x to the next point of x?
Jakob Sørensen
2013-5-13
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 个评论
Howie
2013-5-13
Jakob Sørensen
2013-5-13
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
2013-5-13
Jakob Sørensen
2013-5-13
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
2013-5-14
so why not just create a new array which stores only the required points?
类别
在 帮助中心 和 File Exchange 中查找有关 2-D and 3-D Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!