when datenum is the x-axis How to change the x-axis to discernable time

조회 수: 6(최근 30일)
peter huang
peter huang 2022년 5월 18일
댓글: dpb 2022년 5월 20일
I have a five-year water level data. On the x-axis, I use datenum to convert the time into numbers so that I can draw the graph, but the x-axis is the value of datenum when it is represented. I would like to ask if there is any way to rotate the x-axis. Available year and month
i have water level data 2000 - 2005
I want to display on the x-axis 2000/01 2000/02...2000/12 2001/1...2000/12...2002/1.....2005/12
There are twelve divisions between years
  댓글 수: 2
dpb 2022년 5월 18일
As @Walter Roberson suggests, if your can, use datetime instead and you'll get the date strings on the axis automagically. With datenum, there's a corollary function datetick that will let you set the date string format as you want it --it's somewhat klunky, but it does work.
I would suggest that with six years of data you simply do not have sufficient room for labelled ticks every month; that's 72 ticks which would be difficult even without the length of a date string to display, rotated or not. I'd suggest cutting the number by at least half; it may be convenient that 12 is also divisible by 3 or 4.
If you do/can go with the datetime instead, it's likely the auto tick labels will separate the year from the date string portion at the tick location and place it like a multiplier on a numeric axis to gain the extra room.

댓글을 달려면 로그인하십시오.


dpb 2022년 5월 18일
편집: dpb 2022년 5월 18일
On an assumption you can use datetime
t=datetime(2000,1,1):days(1):datetime(2000,1,1)+calyears(6); % build daily time vector over your range
plot(t,randn(size(t))) % plot against it w/ datetime
xtk=xticks; % get the default axis tick values
xtk=xtk(1):calmonths(1):xtk(end); % make a new set by month
hAx=gca; % the axes handle
hAx.XTickLabelRotation=45; % set the rotation
hAx.XAxis.TickLabelFormat='u/MM'; % your desired format for tick labels
This produces with default figure size the following --
You see there simply isn't enough room physically for that many tick labels -- even if you enlarge the figure, unless it's full screen it's going to be too busy to lookd good.
Just for good measure in case @Walter Roberson's fear is correct, to do the same with datenum would look something like
t=datenum(2000,1,1):datenum(2006,1,1); % days are integer portion of datenum
plot(t,randn(size(t))) % plot against datenum instead
xtk=cell2mat(arrayfun(@(y)eomday(y,1:12),2000:2005,'UniformOutput',false)); % days in month for each year
xticks([t(1) t(1)+cumsum(dtk)]) % set ticks at one-month
datetick('x','yyyy/mm','keepticks','keeplimits') % show as date strings
hAx=gca; % rotate the labels
Looks the same, you'll want to only use every second or third tick...
With datetime axis, you can just reset the tick spacing and the tick labels will update to match; if you are forced or choose to use datenum, then
xtk=xticks; % retrieve present tick values
xticks(xtk(1:3:end)) % use quarterly (every three months)
datetick('x','yyyy/mm','keepticks','keeplimints') % MUST CALL AGAIN!!!
hAx.XAxis.FontSize=8; % will help legibility, too, probably
  댓글 수: 5
dpb 2022년 5월 20일
Yeah, the above is the barebones outline of what a user function would have to do -- to make it work at all well would need to also tie in with callback functions to redraw -- and not sure if can trigger callbacks on all events that would need it...

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by