How to label x-axis with dates?

淺唱寂寞╮ 提交于 2019-12-01 23:07:00

Have you thought about working with the actual date format? This way your time arguments stay in chronological order.

First do some preparations:

days = [22 23 24 25 26 27 28 29 30 31 1 2 3 4];
months = [1 1 1 1 1 1 1 1 1 1 2 2 2 2];
year = repmat(2015,numel(days),1);

in_bed = [3 8 26 76 225 298 258 233 189 128 68 29 14 4];
convalescent = [0 0 0 0 9 17 105 162 176 166 150 85 47 20];

Create a datevec (date vector)

dateVector = [year(:) months(:) days(:)];

and convert it to a double value with datenum:

date = datenum(dateVector); 

Plot and fix the x-ticks

plot(date,in_bed,'*',date,convalescent,'*')
set(gca,'XTick',date)  %// if you leave this step, labeling is dynamic!

Define the format of your labels with datetick:

datetick('x','dd-mm','keepticks')

and voilà:

Check the following link :http://www.mathworks.com/help/matlab/ref/datetick.html

ax1.XTick = xData;
datetick(ax1,'x','mm','keepticks')

ax2.XTick = xData;
datetick(ax2,'x','mmm','keepticks')

First, use the following xData instead of date in plot:

startDate = datenum('01-22-2014');
endDate = datenum('02-04-2014');
xData = linspace(startDate, endDate, endDate - startDate + 1);

Then, set ticks with datetick and keeplimits option:

datetick('x','mm-dd-yyyy','keeplimits');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!