필터 지우기
필터 지우기

How to make a Heatmap Calendar plot

조회 수: 11 (최근 30일)
Emanuel Valdes
Emanuel Valdes 2018년 8월 28일
편집: CdC 2021년 4월 26일
I'm trying to make a Heatmap Calendar for MP2.5 like the one made for R's "Open Air Project", using date and concentration (data attached)
It should look like this:
I know there's a calendar function, but I have no idea of how to plot it. I would really appreciate any help you could give me. Thanks a lot!
  댓글 수: 1
jonas
jonas 2018년 8월 28일
Should be possible but probably requires a lot of code unless you can find something on FEX. you may be able to overlay a heatmap on the calender function.

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

채택된 답변

jonas
jonas 2018년 8월 28일
편집: jonas 2018년 8월 29일
I made something similar to what you were asking for. It's just something I put together for fun, so there are definitely some shortcomings and lots of shortcuts. However, if you don't find anything better on FEX, then you can develop this further. You have to play around with the figure size, markersize and fontsize to make it look good. If you change one you have to adjust the others.
% Load data
data=load('cal.mat')
Date=data.Date;
MP25=data.MP25;
MP25(isnan(MP25))=-100;
% Group data
x=weekday(Date);
y=week(Date);
[G,m]=findgroups(month(Date));
% Figure starts here!
figure;hold on
for ii=1:12
% Extract subgroup of monthly data and name of month
xm=x(G==ii);
ym=y(G==ii);
zm=MP25(G==ii);
Mname = unique(month(Date(G==ii),'name'));
% Initiate subplot ii
ax(ii) = subplot(3,4,ii);hold on
% Write out day of month on grid
str=cellstr(num2str(day(Date(G==ii),'dayofmonth')));
text(xm,ym,ones(size(xm)).*100,str,'fontsize',6,...
'fontweight','bold',...
'verticalalignment','middle',...
'horizontalalignment','center')
zm(zm<0)=NaN;
% Plot color scale
H(ii) = scatter3(xm,ym,zm,150,zm,...
's','filled','markeredgecolor','none');
% Fix some properties that are annoying to change after plotting
title(Mname)
set(gca,'ylim',[min(ym)-.5 min(ym)+5.5],...
'ytick',[min(ym)-.5:max(ym)+.5])
grid on
end
% Add colorbar
colorbar('west','position',[0.93 0.30 0.03 0.4])
% Set up properties for all axes
set(ax,'clim',[0 90],...
'xcolor','k',...
'ycolor','k',...
'xtick',1.5:8,...
'box','on',...
'xticklabels',{'S ','M ','T ','W ','T ','F ','S '},...
'yticklabels',{},...
'ticklength',[0 0],...
'ydir','reverse',...
'xlim',[0.5 7.5],...
'view',[0 90],...
'layer','top');
% Adjust figure size to fit with the font/markersize
set(gcf,'color',[1 1 1],...
'position',[955 140 640 428])
colormap(flipud(autumn))
  댓글 수: 14
jonas
jonas 2018년 8월 31일
No problemo. What do you mean month name local?
This is where the month name is extracted and stored in a cell array:
Mname = unique(month(Date(G==ii),'name'));
you can change the 'name' to 'shortname' ( Jan,Feb ... ) or 'monthofyear' ( 1,2,...), but that's it. You can of course also remove this line and specify any strings manually:
Mname={'Janu','Febr'...,'Dece');
CdC
CdC 2021년 4월 26일
편집: CdC 2021년 4월 26일
Note: the cal.mat file requested in the code is based on the datafile in the original question. Thanks for creating this lovely and helpful code.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Purple에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by