我想显示一个长度为(24*60*60)的数组。x轴是一天中的时间。
当我使用以下代码时,x轴显示00:00:00, 00:59:59, 01:59:59, ..., 23:59:59
,但图上没有绘图。也就是plot(1:length(A),A)
没有作用。(我也试过打开/关闭hold
开关,和24
/24+1
)
A = rand(1,86400);
x0 = datenum('00:00:00','HH:MM:SS');
x1 = datenum('23:59:59','HH:MM:SS');
xData = linspace(x0,x1,24+1);
plot(1:length(A),A);
set(gca,'XTick',xData);
datetick('x','HH:MM:SS','keepticks');
当我尝试以下代码时,可以正确显示plot。然而,x轴变成了00:00:00, 02:24:00, 04:48:00, ..., 21:36:00, 00:00:00
A = rand(1,86400);
x0 = datenum('00:00:00','HH:MM:SS');
x1 = datenum('23:59:59','HH:MM:SS');
xData = linspace(x0,x1,86400);
plot(xData,A);
datetick('x','HH:MM:SS','keepticks');
我也试过这个版本
A = rand(1,86400);
x0 = datenum('00:00:00','HH:MM:SS');
x1 = datenum('23:59:59','HH:MM:SS');
xData = linspace(x0,x1,86400+1);
plot(xData,[A,0]);
datetick('x','HH:MM:SS','keepticks');
x轴仍为00:00:00, 02:24:00, 04:48:00, ..., 21:36:00, 00:00:00
,可以正常显示。
有没有办法将x轴设置为00:00:00, 01:00:00, 02:00:00, ..., 23:00:00, 00:00:00
或00:00:00, 00:59:59, 01:59:59, ..., 23:59:59
?
非常感谢!
问题出现在首先在区间[1,numel(A)]
上绘图,然后在[xData(1),xData(end)]
上设置刻度。
您可以通过在xData
:
A
来修复此问题。A = rand(1,86400);
x0 = datenum('00:00:00','HH:MM:SS');
x1 = datenum('23:59:59','HH:MM:SS');
xData = linspace(x0,x1,24+1);
plot(linspace(x0,x1,numel(A)),A); % change here
set(gca,'XTick',xData);
datetick('x','HH:MM:SS','keepticks');
同时,添加
一行set(gca,'XTickLabelRotation',45); % or some angle
提高了打号标签的可读性。