Lotte
Lotte

Reputation: 3

How to plot dates against prices

I have a dataset with in the first column dates in the form of 'dd-mm-yy' (so for example (15-3-1978) and in the second column prizes. Now I want to plot dates against prices, but unfortunately

plot(dates,prizes) 

doesn't work. Can somebody help me out?

Upvotes: 0

Views: 108

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

Always when working with dates use the functions around datenum and it becomes an easy task:

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


Very convenient is also the datetick function as an alternative option:

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

enter image description here


Regarding your comment, have a further look at datetick's format options you have various options. For a dataset like

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

using above code and additionally:

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

you get e.g.:

enter image description here

Generally this article may be helpful for you as well.

Upvotes: 3

Related Questions