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

조회 수: 3(최근 30일)
Giuseppe D'Amico 2021년 2월 16일
편집: dpb 2021년 2월 16일
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.
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
dpb 2021년 2월 16일
So, what do the start, stop times correlate to????

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

### 답변(1개)

dpb 2021년 2월 16일
편집: dpb 2021년 2월 16일
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표시숨기기 이전 댓글 수: 2
dpb 2021년 2월 16일
"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.

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

### Community Treasure Hunt

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

Start Hunting!

Translated by