# how to make a heatmap for a monthly time serie?

12 views (last 30 days)
Rt Ro on 3 Dec 2019
Commented: Rt Ro on 3 Dec 2019
I have a monthly time series from jan1971 to dec 1986.
I want to make a heatmap to show the value of each month in each region.
whaen I use the heatmap structure, I should write the name of x-values. but they are192 and I can not write all of them.
xvalues = {'Jan-1971', Feb1971',..., 'Dec 1986'};
yvalues = {'A','B','C','D''};
h = heatmap(xvalues1,yvalues1,cdata1);
could you please let me know the best way to show the xvalues?
could you make the heatmap with the attached data?

Kostas on 3 Dec 2019
Based on the heatmap documentation
months = categorical(tbl.Var2);
years = categorical(tbl.Var1);
xlabels = categories(months);
ylabels = categories(years);
nummonths = numel(xlabels);
numyears = numel(ylabels);
x = double(months);
y = double(years);
A = tbl.A;
cdata = accumarray([y,x],A,[numyears,nummonths],@mean,NaN);
h = heatmap(xlabels,ylabels,cdata);

Rt Ro on 3 Dec 2019
t1 = datetime(1971,1,1);t2 = datetime(1986,12,1);
t = t1:calmonths(1):t2;t= t(:);
xlabels = string(datestr(t,12));
ylabels = {'A' 'B' 'C' 'D'};
cdata = tbl{:,3:6};
h = heatmap(xlabels,ylabels,cdata');
cmap = redbluecmap;
newCmap = imresize(cmap, [64, 3]); % original color map contain just 11 colors, this increase it to 64
newCmap = min(max(newCmap, 0), 1);
h.Colormap = flipud(newCmap);
set(h,'XTick',datetime('Jan-1971'):calmonths(12):datetime('Dec-1986'),'FontName', 'Times New Roman','fontsize',12);
I added the last code to show each two years on the x axis. it didn't work.
Kostas on 3 Dec 2019
Unfortunately i do not know how/if this can be done in heatmap. One solution could be to save the image in vector format and then change the labels you want using an image proseccing software.
Something else you could do so as to seem nicer, it would be to make a heatmap for each month seperately.
For example to get just for January you could change the "xDisplayData" as follow
xDisplay = h.xDisplayData;
Jan = xDisplayData(1:12:end);
h.xDisplayData = Jan;
And you can do the same for the rest of the months
% February
Feb = xDisplayData(2:12:end);
h.xDisplayData = Feb;
Rt Ro on 3 Dec 2019
Thank you so much.
another person helped me with this code:
idx = month(t)==1; % index of datetime values to show as x tick
h.XDisplayLabels(~idx) = {''}; % replace rejected tick labels with empties
it works well:)

Sean de Wolski on 3 Dec 2019
Convert your x values to datetime. Then heatmap will just work:
dt = datetime('now'):hours(1):datetime('tomorrow');
v = rand(10,numel(dt));
heatmap(dt, 1:size(v,1), v)

Sean de Wolski on 3 Dec 2019
dt = datetime('now'):hours(1):datetime('tomorrow');
v = rand(2,numel(dt));
y = ["Hello" "World"];
heatmap(dt, y, v)
Sean de Wolski on 3 Dec 2019
Do you want to bin by month? instead of by hours or days?
Rt Ro on 3 Dec 2019
Yes I want to show it by month. I could do it. Now, there are a lot of xdisplay labels (192) and I am trying to show each two year on the x axis not each month. Do you have any idea?