Finding start dates and end dates of a year and plot them

I have this code
t=datetime(2017,1,1) + hours(1:8760); % terminals per hour for the whole year
plot(t, y); % plot the the data versus year's hours
MidleMonths = datetime(year,1:12,15); % desired tick locations, as datetimes
set(gca, 'xtick', MidleMonths);
hold on
Now
I want to:
  1. determine the start date and end date of each month in the above year.
  2. Then, plot dotted lines on the start dates and end dates of the months of the above year.
  3. There is no magnitude (Y values) for the dotted lines (start and end dates). The dotted lines will go to infinity (up to the top of the image).
How can I do it please?
See the target figure ( this dotted lines done on PowePoint)

 Risposta accettata

I would suggest looking into the following functions
  1. dateshift - to shift a date to the start of the month
  2. calmonths - to increment the start date by exactly one month
  3. xline - create a vertical line that continues to infinity
Here's a dummy example to get you started
t=datetime(2017,1,1) + hours(1:8760); % terminals per hour for the whole year
% ignore code for y. Just recreating a shape similar to the original plot
y = 30-(([1:length(t)]-4380).^2)/1e6 + 15*rand([1,length(t)]);
plot(t, y); % plot the the data versus year's hours
% Create vector of dates from start of the first month to end of t incrementing by 1 month
mStart = dateshift(min(t),"start","month"):calmonths(1):max(t)
% Add vertical line for start of each month
for l = 1:length(mStart)
xline(mStart(l),'--')
end
% Set display format of dates on x-axis
xtickformat(gca,"MMM yy")

8 Commenti

Thanks alot, you solved the issue.
Is it possible to keep each month's background with a certian colour for the whole month like this example:
An xline has a color and label property, though creating exactly what you show will likely not be as straightforward as using those options. The xline documentation will show you how to use them.
Another option might be to use the text function.
I think the xline is to colour the dotted line, while I need to colour the whole area from first of month till end of the month with one colour.
Example:
The area limited between 1st January till 31st January should have one colour (red as in the figure example).
See below, but I need the red colour to be in the background so I can see the results of Y axis at eac point in X-axis)
Ah, that's different from what you showed previously. In that case, look into using patch. Patch does not accept dates as X or Y values, but it looks like the numeric equivalent is 'dayofyear'.
Adjusting the 'FaceAlpha' property sets the transparency.
HG-NU
HG-NU il 9 Mag 2020
Modificato: HG-NU il 9 Mag 2020
Thanks Cris.
I think I need your assistance as well please :)
I can find the x values for each month (start and end), but I want the background colour up to the topest point in Y axis for all months. So I can then use Patch function.
Also, X values are date, while the Patch function does not deal with dates. I think X values need to be number.
Can you assist of how to do this please?
Yes, I mentioned the datatype issue for X values in the post you responded to. Use the day function with dayType set to dayofyear.
For Y, I would get YLim values and arrange them to be your four Y values for patch.
Not sure how far you got. Here's how I might do it.
% determine where to put vertical lines
mStart = dateshift(min(t),"start","month"):calmonths(1):max(t);
% get a unique color for each month using the jet colormap
C = jet(length(mStart)-1);
for l = 1:length(mStart)
xline(mStart(l),'--')
% Add colored patch for each month.
if l>1
% Get 4 X coordinates for patch
d1=day(dateshift(mStart(l-1),'start','month'),"dayofyear")-1;
d2=day(dateshift(mStart(l-1),'end','month'),"dayofyear");
xBox=[d1 d2 d2 d1];
% Get 4 Y coordinates for patch
yBox=sort([get(gca,'YLim') get(gca,'YLim')]);
% Add patch. Set transparency by adjusting FaceAlpha.
hold on
patch(xBox,yBox,C(l-1,:),'FaceAlpha',0.2,'EdgeColor','none')
hold off
end
end
Thanks Cris, for your amazing assistance. I really appreciate it all.
:)

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2020a

Richiesto:

il 8 Mag 2020

Commentato:

il 11 Mag 2020

Community Treasure Hunt

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

Start Hunting!

Translated by