【问题标题】:Matlab: Intraday Time Series Plotting IssueMatlab:日内时间序列绘图问题
【发布时间】:2017-01-12 17:22:25
【问题描述】:

您能帮我解决以下问题吗?

我有一个大型的盘中财务数据集。更具体地说,连续多天每 15 分钟收盘价。我在绘制数据的时间序列时遇到了问题。 这是我的系列的一个例子:

'29-Dec-2016 15:00:00'  62.8400000000000
'29-Dec-2016 15:15:00'  62.8300000000000
'29-Dec-2016 15:30:00'  62.8900000000000
'29-Dec-2016 15:45:00'  62.8550000000000
'29-Dec-2016 16:00:00'  62.8900000000000 (Closing of the market)
'30-Dec-2016 09:45:00'  62.7300000000000 (Opening of the market)
'30-Dec-2016 10:00:00'  62.2900000000000
'30-Dec-2016 10:15:00'  62.2400000000000
'30-Dec-2016 10:30:00'  62.0900000000000
'30-Dec-2016 10:45:00'  62.1100000000000
'30-Dec-2016 11:00:00'  62.3000000000000
'30-Dec-2016 11:15:00'  62.2300000000000

如果我绘制上面的子样本,matlab 绘图将具有如下图所示的形式:

如您所见,Matlab 绘图在横轴上填充了收市和开市之间的时间,这使得价格看起来“拉长”。

相反,如果我使用增加的观察数(例如 1 到 100...),问题就会被消除,如下图所示:

有没有办法避免价格“拉长”并且在我的水平轴上仍有时间?

提前致谢。

【问题讨论】:

  • 在收盘价之后和/或开盘价之前插入带有时间戳的 NaN 值。或者单独绘制每一天。
  • 不可能单独绘制每一天,因为我有 6 年的 5,10 和 15 分钟数据。此外,如果我要在收盘价向量中添加 NaN 值,那么价格向量将具有我所拥有的时间变量的不同维度。
  • 仔细阅读评论:“在收盘价后插入NaN带时间戳”。当任一端为 NaN 时,Matlab 不会绘制线段,这正是您想要的。我也不明白为什么单独绘制天数是不可能的 - 这只是大约 6*255 个时间序列。

标签: matlab plot timeserieschart


【解决方案1】:

你可以这样做:

首先绘制价格数据

plot(price)

然后设置XTickLabel:

set(gca,'XTickLabel',datevector)

这将使用您的数据设置 X 轴

你可以把它放在一个函数中

function plotprices(data)

datevector = data(:,1);     %store dates in first column
price = num2cell(data(:,2)); %store prices in second column
plot(price)
set(gca,'XTickLabel',datevector)

【讨论】:

    【解决方案2】:

    您可以读取绘图上 x-ticks 的位置,并用您自己的字符串替换它们的标签。所以,假设:

    a) y 有股票价格,并且

    b) Date 有日期字符串, 您可以在第二个情节的末尾添加以下代码以获得接近您想要的内容:

    % limit the x-axis such that all ticks are within the data range
    xlim([1, length(y)]);
    
    % read the marks of the x-ticks
    xt=get(gca, 'XTick');
    % this would place the x tick marks at the same locations 
    %  that Matlab chose by default. If you want to place them
    %  at some other points, just assign those points to xt, e.g.
    %   xt = (1:10:length(y))
    
    % replace the labels of the marks
    set(gca, 'XTick', xt); % rewrite this in case you modify xt
    set(gca,'XTickLabel',Date(xt))
    

    顺便说一句,一个可能更简单的替代方法是使用您的第一个绘图,但不是实线,而是仅使用标记。比如plot(Date, y, '.');

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-24
      • 2020-07-15
      • 2020-12-29
      • 1970-01-01
      • 2022-01-13
      • 2019-12-21
      • 1970-01-01
      • 2016-11-02
      相关资源
      最近更新 更多