为了达到这样的效果,我通常用 s 填充缺失的数据NaN
,如下所示:
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
ory
数据为NaN
s 的位置绘制绘图点。在您的情况下,您可以将缺失的时间点添加到您的时间数据(以具有正确的间隙)并将NaN
s 添加到相应的 Y 值。
顺便说一句,你为什么不绘制两个单独的图,第二个的 X 数据正确移动?
编辑
案例 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;