将纪元秒用作X轴值时,如何格式化X轴刻度标签?

问题描述

我有以下数据集:

octave:42> yy
yy =

   1420174800.00000           21.70000           21.93000           21.64000           21.81000      1949882.00000
   1420434000.00000           21.92000           22.23000           21.90900           22.17000      3189214.00000
   1420520400.00000           22.15000           22.51000           22.08000           22.41000      4810496.00000
   1420606800.00000           22.21000           22.27500           22.07000           22.11000      2397372.00000
   1420693200.00000           21.93000           21.93600           21.70000           21.73000      2797149.00000
   1420779600.00000           21.68000           21.98000           21.68000           21.90000      2377333.00000
   1421038800.00000           21.88000           22.15000           21.87000           22.07000      2347871.00000
   1421125200.00000           21.94000           22.30000           21.76500           22.15000      5255049.00000
   1421211600.00000           22.40000           22.51900           22.24000           22.25000      5843980.00000
   1421298000.00000           22.18000           22.48000           22.14000           22.47990      3498303.00000

如果将其绘制出来,将得到以下图表:

octave:44> plot(yy(:,1),yy(:,5))

Chart

按照八度文档(https://octave.sourceforge.io/octave/function/datetick.html),我尝试了以下方法来显示年份,而不是UNIX纪元秒值。

octave:48> plot(yy(:,5))
octave:49> datetick('x','YYYY')

但这是结果:

enter image description here

我想学习如何显示年份(YYYY)和年份和月份(YY-MM)。我不确定Matlab解决方案是否也适用于Octave。

解决方法

简短版本

plot( arrayfun( @(x) datenum([1970,1,x]),yy(:,1) ),5) )
datetick( 'x','yyyy' )

说明

您拥有的数字是unix纪元(即自1970-1-1 00:00:00 UTC以来的秒数)。 之所以要从datetick中获取奇怪的数字,是因为它不希望将unix历元作为输入,而是“日期数字”(自UTC 0000-1-0 00:00:00以来的小数天数,无论什么意思)。

您可以做的是使用您的值通过datevec转换为datenums。例如。您的第一个值,即1420174800,可以像这样转换为datenum

datenum([1970,1420174800])   % i.e. the 1420174800th second of 1970-01-01 
% ans = 7.3597e+05

datestr( datenum([1970,1420174800]),0 )
% ans = 02-Jan-2015 05:00:00

因此,您可以将所有unix纪元转换为datenums,例如像这样

DateNums = arrayfun( @(x) datenum([1970,1) )

然后使用DateNums进行绘制,并按预期使用datetick。

以上解决方案在八度音程和matlab中将完全相同。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...