Plot several series in the same chart

1 回表示 (過去 30 日間)
Alex
Alex 2012 年 8 月 8 日
Hello
Let's say I have a date vector X of length 100 that I want to use as x-axis in my plot. I also have another vector Y of length 100. I can thus plot Y as a function of X. I also have third vector Y2 which I want to plot on the same chart. The problem is that the length of Y2 is smaller, say 95, because there are no observations on the first 5 days. So, I want to make a plot where I plot Y and Y2, so that Y2 starts later (at X5).
How can I do this? Thank you very much for your time and consideration!
Alex

回答 (2 件)

Isktaine
Isktaine 2012 年 8 月 8 日
You can still plot them on the same graph, you just need to make your x vector smaller. For example
%Plot your first 100 points
plot(X,Y1)
%Keep the axis and data
hold on
%Removes first 5 elements of x where there's no corresponding Y data
smaller_x=x(6:end)
%Plot Y2 data, 95 points against a new X vector
plot(smaller_x,Y2)
%turn off hold so you can create new plots
hold off
Does this make sense?

Matt Fig
Matt Fig 2012 年 8 月 8 日
You can pad with nans to stand for no observation. For example:
% Let's create some data. Say this data is the given:
x = 1:10;
y1 = x.^2;
y2 = (6:10).^2.1;
% Note that y2 does not have observations for 1:5. So we pad it:
y2p = [nan(1,5) y2];
plot(x,y1,'r',x,y2p,'b')

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by