Plotting specified date intervals

I have two date intervals: from
25-July-2017 from 9:00 AM to 25-July-2017 12:30 PM and from
26-July-2017 from 9:00 AM to 26-July-2017 12:30 PM.
I am not able to remove the duration in between (from 12:30 PM to the next day 9:00 AM).
A = readtable('Time and Sale_7_25_2017.xlsx');
B = readtable('Time and Sale_7_26_2017.xlsx');
A.Date = datetime(A.Date,'ConvertFrom','excel');
B.Date = datetime(B.Date,'ConvertFrom','excel');
figure(1)
plot(A.Date, A.Price,B.Date, B.Price, '-r', 'linewidth', 2);
datetick('x', 'dd-mmm HH:MM:SS','keeplimits','keepticks')
xlabel('Period')
ylabel('Price')
grid on
grid minor;

8 个评论

Similar question was answered in the following. Though this Q&A is written in Japanese, images and MATLAB scripts will be helpful.
dpb
dpb 2017-7-31
编辑:dpb 2017-7-31
Seems like requires a lot of work -- would think TMW would have developed weekday functions specifically for such purposes already.
ADDENDUM Misread the dates; was thinking it was a Friday/Monday gap hence reference to weekends. Albeit same issue of gap in data is going to happen if use absolute times for the axis.
That doesn't fix the problem !
Why not? While convoluted, it does generate a plot with the two sections more closely spaced.
What, specifically, do you think required to "fix" the problem?
If you mean to have the two series connected by continuous line, the simplest alternative would be to use the basic idea of the first solution but instead of using two segments to go ahead and concatenate them as
price=[A.Price;B.Price]; % combine price vectors
plot(price); % plot against ordinal number
then set the ticks and tick labels as wanted; but the ticks will be 1:N instead of actual dates.
As presently implemented, there's no way to eliminate a section of an axis; if you set the actual dates as numeric values the limits will have to encompass the full range of real dates and the values will be placed where they belong in absolute magnitude relative to that.
Duration arrays have the same problem, when you compute the difference the two days between the two sets will be maintained. You could have two duration arrays zero-based from the beginning of the two segments, and then shift the second from the end of the first but that essentially is what using the ordinal position does as you would still have to label the tick values with actual dates manually.
It is, agreed, klunky but I couldn't see that even the Financial Toolbox has a good solution prepackaged; their examples simply interpolate for holidays and draw a continuous curve with fake data to make it look continuous instead of actually handling the business days.
Thank you. So do you think it's doable... that is to join the two lines togther (i.e. removing the empty duration in between) if so, can you provide the code.
Plotted as native dates or durations, no; other than by one of the subterfuges shown or similar.
As for the other, the code is shown above -- join the two vectors into one and plot versus the ordinal number. Then, as the second answer in the other thread shows, label the ticks with the dates (or just use text() to write the dates where ticks are).
So did the example below provide the desired result?

请先登录,再进行评论。

 采纳的回答

dpb
dpb 2017-8-1
编辑:dpb 2017-8-1
What I'm suggesting...
dn=datetime(['25-July-2017 9:00 AM'; '25-July-2017 12:30 PM']); % start/stop times day 1
dn=[dn(1):minutes(1):dn(2)].'; % datetime vector for day 1
p1=randn(size(dn)); p2=randn(size(dn)); % arbitrary data to go along for two days
A=table(dn,p1); % make a table similar to yours for demo purpose
dn=dn+1; B=table(dn,p2); % ditto, second day time vector (1 day later)
plot([A.p1;B.p2]) % plot the price data combined vs ordinal position
xlim([1 2*length(dn)]) % set the plot limits 1:N points total
hAx=gca; % get/save the axes handle
xt30=[1:30:211 212:30:422]; % indices of 30-minute intervals in ordinal vector
hAx.XTick=xt30; % set those tick marks to match
dntot=[A.dn;B.dn]; % the whole time vector concatenated
dastr=datestr(dnTot(hAx.XTick),'ddmmm HH:MM'); % convert to string represenation
dastr(8,:)=blanks(size(dastr,2)); % clear the point w/ almost overlaying ticks
hAx.XTickLabel=dastr; % write the tick labels as time strings
hAx.XTickLabelRotation=45; % rotate to give some room
grid on
NB: There are two ticks at the end of first day and beginning of second that are only a single point apart so there simply isn't enough room to label both. Hence the clearing of the last label in the first day to make room for the first in the second day.
You could make room by inserting some blank space but then you're back to the same issue with the use of absolute time -- as someone once said "time is what keeps everything from happening all at once" -- you've got a problem that if you want the two days data to appear continuous then there's no place to put labels for the two points that aren't consistent in their position with the other spacing...the above is one way to work around that; label the beginning of second day but not the last point of the first.
There is the double line from the grid that does make a slight demarcation where the day end is; that may be useful in reading the plot; you'll have to choose whether you want to keep the two adjacent points or just eliminate one entirely.
ADDENDUM
If you want to retain the color separation between the two days (probably not a bad idea; will help out some on the separation issue) change the plot statement just slightly...
plot([[A.p1;nan(size(A.p1))] [nan(size(A.p1));B.p2]])
creates two series of full length with alternate halves of NaN to keep their relative places. Alternatively, you can use the x-positions explicitly--
x=[1:length(A.p1)].';
plot(x,A.p1,x+length(A.p1),B.p1)

4 个评论

Interesting approach, however in this code the time (data) set as steps. what if for example sells occur at different times. How to plot price Vs time.
dpb
dpb 2017-8-3
编辑:dpb 2017-8-3
Well, you're back to the same issue...what do you want; actual time or pseudo-time eliminating closed-market times from the plot entirely? Or something else?
The way TMW graphics work is basic--the points are drawn at the values of the two axes; if you don't want them to be in positions that are directly tied to the numerical position of the data, then you have to either be able to transform the scale or otherwise manipulate the location value at which the value is plotted such that that value has the relative positions desired.
But, if any such scaling is used, the default tick positions and labels will reflect those numerical values identically. You can label ticks arbitrarily, but that has to be done specifically as is the case above.
The other alternative is the one shown in the previously-referenced answer to have multiple axes that are disjoint in their data ranges and place and scale them to produce the desired visual effect.
Excellent code. Thank you for clarification. Great help
You're welcome...it wouldn't be a bad enhancement request to submit to TMW for the ability to specify a break in an axes between two limits as a higher-level capability. It might be to mimic how NaN values aren't plotted an option that the user could select whether the axis were to be shown as well with a secondary option on top of that to actually eliminate the resulting white space...could get complicated, but there's often a need for such kinds of specialized axes.

请先登录,再进行评论。

更多回答(1 个)

类别

帮助中心File Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by