Help with Box plot

1 次查看(过去 30 天)
sc1991
sc1991 2018-1-2
评论: sc1991 2018-1-2
Hi I am working with some data which has three column.(Unit Name, Date, TotalData). I have to make box plot month wise. So from this data I have to extract each month and then further extract each day and plot a combined box plot for all the days of that month. I have a script which can plot it but its not dynamic. Can anyone suggest what Can I do to further improve the script.
mydate=cellfun(@(x) x(1:end-9),Date,'un',0);
temp=table(UnitName,mydate,TotalData,Software,abs_time);
temp=sortrows(temp,5);
mydata=table2cell(temp);
mydate=mydata(:,2);
uniquedate=unique(mydate);
[~,start_idx]=ismember(uniquedate,mydate);
end_idx=ones(length(start_idx),1);
for i =1:length(start_idx)
if i==length(start_idx)
end_idx(i,1)=length(mydate);
else
end_idx(i,1)=start_idx(i+1)-1;
end
end
for i=1:length(uniquedate)
day_number=day(uniquedate(i));
[~,month_name]=month(uniquedate(i));
eval(sprintf('%s%d=TotalData(start_idx(i):end_idx(i));',month_name,day_number));
Box{i,1}=sprintf('%s%d',month_name,day_number);
end
%For Box plot
Box=[jan24;jan25;jan26;jan27;jan28;jan29;jan30;jan31];
BoxFinal=[ones(size(jan24));2*ones(size(jan25));3*ones(size(jan26));4*ones(size(jan27));...
5*ones(size(jan28));6*ones(size(jan29));7*ones(size(jan30));8*ones(size(jan31))];
boxplot(Box,BoxFinal);
grid on
set(gca,'XTick',1:1:31,'fontsize',10)
set(gca,'XTickLabel',{'jan24','jan25','jan26','jan27',...
'jan28','jan29','jan30','jan31'})
xlabel('Dates')
ylabel('TotalData')
title('Jan data for Unit - UnitName , Software xyz')
So in this example I am taking jan so I am able to filter out the days of the month and further extract data for each day and make them as a variable ex: Jan24,jan25. But when it comes to box plot I have to write these dates manually as seen in the code and the steps that follow. Can please someone help how can i do it or do you have a better approach.
  1 个评论
sc1991
sc1991 2018-1-2
I did find a way to do it. I will post my code hope it helps someone who gets stuck with similar issue. I know its not very efficient but it works. Please I am open to any suggestion feel free to comment any changes you feel to be made to the code.
% to get box plot based on time series data
mydate=cellfun(@(x) x(1:end-9),DateTime,'un',0);
temp=table(UnitName,mydate,TotalData,Software,abs_time);
temp=sortrows(temp,5);
mydata=table2cell(temp);
mydate=mydata(:,2);
uniquedate=unique(mydate);
[~,start_idx]=ismember(uniquedate,mydate);
end_idx=ones(length(start_idx),1);
for i =1:length(start_idx)
if i==length(start_idx)
end_idx(i,1)=length(mydate);
else
end_idx(i,1)=start_idx(i+1)-1;
end
end
for i=1:length(uniquedate)
day_number=day(uniquedate(i));
[~,month_name]=month(uniquedate(i));
eval(sprintf('%s%d=TotalData(start_idx(i):end_idx(i));',month_name,day_number));
days{i,1}=sprintf('%s%d',month_name,day_number);
end
month_name={'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'};
Box1={};
BoxFinal1={};
for ii=1:length(month_name)
possiblevars=who('-regexp',month_name{ii});
currentvars=possiblevars;
for iii=1:length(currentvars)
Box=eval(currentvars{iii});
Box1=[Box1;Box];
BoxFinal=iii*ones(size(eval(currentvars{iii})));
BoxFinal1=[BoxFinal1;BoxFinal];
end
Box1=vertcat(Box1{:});
BoxFinal1=vertcat(BoxFinal1{:});
Figure1=figure;
boxplot(Box1,BoxFinal1);
grid on
set(gca,'XTick',1:1:31,'fontsize',10)
set(gca,'XTickLabel',currentvars)
xlabel('Dates')
Box1={};
BoxFinal1={};
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by