爲了達到這樣的效果,我通常填充缺失的數據與NaN
S,喜歡這裏:
x = linspace(0,2*pi,100);
y = sin(x);
y(20:30) = NaN; % there will be a gap from point#20 to point#30
plot(x,y);
的原因是MatLab沒有繪製任何x
或y
數據爲的繪圖點s。 在您的情況下,您可能會將缺少的時間點添加到您的時間數據(具有corect間隙)並將NaN
s添加到相應的Y值。
順便說一下,爲什麼不用兩個單獨的地塊繪製第二個X數據正確移位?
EDIT
情況1:你的X-數據是相對於一天的開始時間(在0-24間隔)。如果直接繪製它們,它們會重疊。你必須添加一些手動偏移,這樣的:
% generate test data
x1 = linspace(0,1,25); % 25 points per first day
y1 = rand(25,1);
x2 = linspace(0,1,25); % 25 points per second day
y2 = rand(25,1);
% plot them as two separate plots
% so line style, color, markers may be set separately
XOffset = 3;
figure;
plot(x1,y1,'*k-', x2+XOffset,y2,'*r-');
% plot them as single separate plot
% so line style, color, markers are the same
figure;
plot([x1(:); NaN; x2(:)+XOffset],[y1(:); NaN; y2(:)],'*k-');
% One NaN is enough to insert a gap.
案例2:你的X-數據有包括日期專職信息(如Matlab的序列日期數字,看到now
功能的幫助,例如)。然後只需繪製它們,它們將自動偏移。
% generate test data
XOffset = 3;
x1 = linspace(0,1,25); % 25 points per first day
y1 = rand(25,1);
x2 = linspace(0,1,25)+XOffset; % 25 points per second day, with offset
y2 = rand(25,1);
% plot them as two separate plots
% so line style, color, markers may be set separately
figure;
plot(x1,y1,'*k-', x2,y2,'*r-');
% plot them as single separate plot
% so line style, color, markers are the same
figure;
plot([x1(:); NaN; x2(:)],[y1(:); NaN; y2(:)],'*k-');
% One NaN is enough to insert a gap.
代替
plot(x1,y1,'*k-', x2,y2,'*r-');
你可以做到這樣也(次數的曲線是不侷限於):
hold on;
plot(x1,y1,'*k-');
plot(x2,y2,'*r-');
hold off;
非常感謝。我正在努力在數據中插入NaN以填充空間以獲得期望的結果。但是,我不知道如何在一個時間軸上繪製2個獨立的繪圖,第二個繪圖像您提到的那樣移動?任何指針?再次感謝,Jon – shoo
我更新了我的答案。查看編輯。 – anandr