# how to create a chart with two x axes, one of which with month / day

9 views (last 30 days)
Giuseppe D'Amico on 16 Feb 2021
Edited: dpb on 16 Feb 2021
x1 = linspace(0,17508,17508);
plot(x1,target_2019,x1,output_2019)
xlabel("quarter hour")
ylabel("Reactive Power")
title("Serie storica potenza reattiva 2019")
Hi everyone, I need some expert help. I have to make some plots for a presentation. At the moment I create the graph with the commands listed above. The data I use are quarter-hourly power measurements. What I would like is to insert another x-axis perhaps above in which to insert the indications on the month and day in which the measure falls. For example 01/01 then 02/01 and so on.
Is there any possible way? Even if you enter the entire date, the important thing is that it remains legible.
An infinite thanks to those who can help me.
Giuseppe D'Amico on 16 Feb 2021
the indication of month and day would be enough for me
dpb on 16 Feb 2021
So, what do the start, stop times correlate to????

dpb on 16 Feb 2021
Edited: dpb on 16 Feb 2021
Given you have 2019 in the variable names but won't give actual start/stop dates/times...
x1=linspace(0,17508,17508)
is a 17,508 element vector. At 15 minutes from beginning of 2019, this would be:
t=datetime(2019,1,1):minutes(15):datetime(2019,7,1)+(17508-17377)*minutes(15);
where 17,377 is length of the series from 1/1/2019 thru 7/1/2019; I just returned it first to get the number of elements to see how many more were needed to match up.
>> t.Format='default'; % show what get for time vector...
>> [t(1) t(end)]
ans =
1×2 datetime array
01-Jan-2019 00:00:00 02-Jul-2019 08:45:00
>>
Given the above, then
plot(t,randn(size(t)))
xlim([t(1) t(end)+days(1)])
produced
with monthly ticks/labels as dates. Salt to suit...
Still think things would be easier with durations and datetimes and probably the timetable would handle a lot of this for you automagically, but without further context specific code isn't feasible...
This is somewhat problematic given the behavior of the datetime axis object as still being pretty new to the fold it isn't yet always as friendly as it could be...starting with a numeric axes at the bottom, the process is
x=0:17507; % your x1 on even minutes (you spread interval 0-17508 --> 17509 over 17508 elements)
t=datetime(2019,7,1)+minutes(15)*[0 17507]; % equivalent times @ 15-min intervals
figure
hL(1)=plot(x,randn(size(x))); % initial plot, save line handle
xlim([x(1) x(end)]) % set xlim to range of values
hold on % don't wipe it out
hAx=gca; % get handle to this axes
hAx(2)=axes('Position',hAx.Position','color','none'); % make new axes on top
hL(2)=plot(hAx(2),t,nan(size(t))); % a fake line to create datetime axes
set(hAx(2),'Color','none','XAxisLocation','top','YTick',[]); % fixup the new axes
xlim(hAx(2),[t(1) t(end)]) % make spans match
This yields
It takes the dummy line to turn the default numeric axes into the datetime axes; you can't set datetime limits on a numeric axes directly. This is what isn't yet as transparent as could be with the new-fangled axes. Still much better than fooling with the old datenum and dateticks mess.
A little cleanup could include
set(hAx,'Box','off') % get rid of conflicting tick marks on opposite axes
xline(hAx(1),x(end),'k-'); % add the RH border that also goes away by above
##### 3 CommentsShow 1 older commentHide 1 older comment
Giuseppe D'Amico on 16 Feb 2021
measurements start from 01/07/2019 to 31/12/2019.
I need to keep the two x and y axes I had created and insert an additional x axis above with the date indication as you did
dpb on 16 Feb 2021
"It takes the dummy line to turn the default numeric axes into the datetime axes; you can't set datetime limits on a numeric axes directly."
There was a thread here not too long ago about some undocumented ways to set the type of the axes but I didn't think to bookmark the thread and a quick (like 5-sec) one-time search didn't uncover it so I took the expedient way out.
TMW needs to introduce a settable property to allow user to specify the type of axes object wanted or make the supporting functions like xlim more able to do what is needed under the hood transparently.