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

1 个评论

I am asking for two data sets(matrices), not functions. The rules are a little bit different. so if the time has a dimension different than the data, it simply cannot plot. Thanks, Howie

请先登录,再进行评论。

回答(2 个)

plot(t,x)
hold on
plot(t2,y)

14 个评论

No, you cannot do that to vectors as I believe. x,y are merely matrices.
I think you'd better give samples of the data points (for example, [1,5,10]) instead of t=1:10.
well, sorry for not phrasing it well. The question is how to plot data points on a selected time period, just as functions. I think there must be a way. Of course your method works well for few points, but I guess not for too many cause it needs human to set things right manually?
I don't think so. u can add loops for auto-plotting
I am quite a noob so would u mind just show an example of how to do that? I could be deadly wrong on that matter...
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
I see. with a loop that should get what I want(with first few elements of y off).
If I understood the algorithm it plots single data point one-by-one
but what if I want piecewise linear line to go through all of the t and y separately?
The function plot(x,y) plots y vs. x. Thus, no matter what you wanna plot, define the specific x and y.
what I want is x,y in the same plot against t1, but with first few elements of in y:
>> for i=1:size(t1,2)
if i>2
plot(t1(:,i),x(:,i), 'o')
plot(t1(:,i),y(:,i),'o')
else
hold on
plot(t1(:,i),x(:,i),'o')
end
end
Now, the question is how to connect x and y separately...
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
for some strange reason there're twp plots jumping out. Also how we connect points with piecewise linear line......
Too late tonight... will reply tomorrow morning... Thanks for all the helps ;) the idea is very good, it's just it can't give me two continuous lines...
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?
yes! connecting all thye x; connecting all the y
connect x to y, or connect a point of x to the next point of x?

请先登录,再进行评论。

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 个评论

I think it only works for functions, not data points
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.
>> x = 1:10; >> y = [0 0 3:10]; >> t1 = 1:10; >> t2 = 3:10; >> plot(t1,x,t2,y) Error using plot Vectors must be the same lengths.
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.
So here comes the problem. if I just do that, then 0-values of y will be plotted. but all I want is 3:10. I do not think I can do it just by selecting columns since my goal is to remove the points, without changing the position of the points
(I want the first point of y to be(3,3), not (1,3)).
Maybe I was doing it all wrong.
so why not just create a new array which stores only the required points?

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 2-D and 3-D Plots 的更多信息

提问:

2013-5-13

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by