How to plot event frequency using datetime array in matlab -
i have array of values in datetime format. each value represent event happening @ specified date , time. how can plot event frequency in events per day, events per month, etc?
i managed plot events per hour of day using histogram(mydata.hour)
thank answers!
edit: precisions after first answer below:
yes, that's i'm doing using histogram , data.hour. however, want compute the average number of event per day, , plotting along time period events are.
here working example:
% generating 500 random events dates = datetime(now-1000*rand(500,1),'convertfrom','datenum'); figure; edges = -0.5:23.5; histogram(dates.hour,edges) title('events per hours of day') xlim ([-0.5 23.5]) ax1 = gca; ax1.xtick = 0:2:23; ax1.xticklabel = {'midnight','2','4','6','8','10','noon','14','16','18','20','22'}; ax1.xticklabelrotation = 45; figure; daynumber = weekday(dates); histogram(daynumber) title('events per days of week') ax2 = gca; ax2.xtick = [1:7]; ax2.xticklabel = {'sunday','monday','tuesday','wednesday','thursday','friday','saturday'}; ax2.xticklabelrotation = 45;
assuming have array of datetimes already, can access day, hour, minute, second, etc. of each datetime object with:
datetime.day datetime.hour datetime.minute
using notation, can use simple array keep count of how many events happen @ each day/hour/minute/etc. , use bar graph plot results. 2 examples shown below , can extrapolate these modify need.
here's how work plotting event frequency every hour in 1 day:
hours_count = zeros(24,1); dt = 1:length(datetimes) hour = datetimes(dt).hour; hours_count(hour+1) = hours_count(hour+1) + 1; end bar(hours_count) set(gca,'xtick',1:24,'xticklabel',strtrim(cellstr(num2str([0:23]')))) xlabel('hour of day') ylabel('number of events')
here's how work plotting event frequency every day in 1 month:
days_in_the_month = 30; days_count = zeros(days_in_the_month,1); dt = 1:length(datetimes) day = datetimes(dt).day; days_count(day) = days_count(day) + 1; end bar(days_count) set(gca,'xtick',1:days_in_the_month,'xticklabel',strtrim(cellstr(num2str([1:days_in_the_month]')))) xlabel('day of month') ylabel('number of events')
Comments
Post a Comment