如何根据价格绘制日期

时间:2015-04-28 15:04:26

标签: matlab plot matlab-figure

我的第一列日期中的数据集的格式为'dd-mm-yy'(例如(15-3-1978)和第二列奖品。现在我想根据价格绘制日期,但是不幸的是

plot(dates,prizes) 

不起作用。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:3)

使用日期时始终使用datenum周围的功能,这将变得轻而易举:

dates = {'15-3-1978';
         '16-3-1978';
         '18-3-1978'}

prizes = [ 42 
           19
           84 ]
datesNum = datenum(dates,'dd-mm-yyyy')
datesStr = datestr(datesNum)

plot(datesNum,prizes)
set(gca,'XTick',datesNum)
set(gca,'XTickLabel',datesStr)

enter image description here

非常方便的是datetick功能作为替代选项:

plot(datesNum,prizes)
datetick('x','dd-mm-yyyy')

enter image description here

关于您的评论,请进一步了解datetick's format options您有多种选择。对于像

这样的数据集
dates = {'15-3-1978';
         '16-4-1978';
         '18-5-1979'}

使用上面的代码并另外:

plot(datesNum,prizes)
datetick('x','QQ-yy')

你得到例如:

enter image description here

Generally this article may be helpful for you as well.