【问题标题】:X-axis dates are wrong in Matlab plotMatlab图中的X轴日期错误
【发布时间】:2015-07-02 02:33:20
【问题描述】:

我的日期格式为“yyyymmdd”。当我把这些放在我的情节的 X 轴上时,我得到了疯狂的数字。我的绘图间隔是最近十年,但 X 轴刻度标签显示 1979 年和其他奇怪的数字。谁能指出我正确的方向来纠正这个问题?谢谢。

编辑:这是请求的代码:

TradeDate=TradeDate+693960; % convert excel serial dates to matlab serial dates
TradeDate=datestr(TradeDate,'mmddyyyy'); % convert the format into yyyymmdd
TradeDate=str2double(cellstr(TradeDate)); % convert the date strings first into cell arrays and then into a double

plot(TradeDate,beta);
xlabel('Date');
ylabel('Beta');
daspect([300 1 1]);
set(gca,'xtick',linspace(TradeDate(1),TradeDate(1715),50));
ax=gca;
ax.XTickLabelRotation=45;

【问题讨论】:

  • 现在我有了在年份的第一个数字之后带有句点的刻度标签(即 20080117 显示为 2.0081。我正在玩 datetick 但到目前为止没有任何成功。
  • 你能告诉我们你的代码来重建你的问题吗?我的猜测是您使用的是 x 轴上的原始日期数字。您只需使用 datestr 将它们重命名为正确的日期。
  • @rayryeng 查看上面发布的代码,谢谢。
  • 该代码无法自行运行。有未定义的变量......就像1Date。此外,TradeDate 未使用。
  • @rayryeng 抱歉 TradeDate 应该代替 1Date。

标签: matlab date axis-labels


【解决方案1】:

当我怀疑您实际上想自己绘制日期字符串时,您正在做的是在 x 轴上绘制序列日期号。

因此,首先使用序列日期编号生成绘图,然后通过更改 x-axis 标签来使用日期字符串。顺便说一句,您使用 str2double 的代码毫无意义,因为如果我正确地按照您对第一行代码的评论,这已经是一个序列日期。

类似这样的:

TradeDate=TradeDate+693960; % convert excel serial dates to matlab serial dates

%// Note change in variable name here and convert to cell array of strings
TradeDates=cellstr(datestr(TradeDate,'mmddyyyy')); % convert the format into mmddyyy

plot(TradeDate,beta);
xlabel('Date');
ylabel('Beta');
daspect([300 1 1]);
set(gca,'xtick',linspace(TradeDate(1),TradeDate(1715),50));
%// Change - Change x-axis labels
set(gca, 'XTickLabel', TradeDates(1:50:1715));
ax=gca;
ax.XTickLabelRotation=45;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多