Use timetable as x axis in heatmap

Hello,
I have this time table (t_avg) and I want to use it as the x axis for a heatmap of my load data (load_avg).
I am currently using this code but the x axis is not the correct time:
figure (4)
h=heatmap(load_avg');
XLabels = 1:size(load_avg,1);
CustomXLabels = string(XLabels);
CustomXLabels(mod(XLabels,10) ~= 0) = " ";
h.XDisplayLabels = CustomXLabels;
caxis([0 max(max(load_avg))])
colormap jet
xlabel('Time');
ylabel('Unit');
I tried using this code but it didn't work:
heatmap(t_avg,'XVariable','Time',load_avg');
I'd appreciate it if any one can help.
Thanks,
Amir

 Accepted Answer

Peter Perkins
Peter Perkins on 11 Mar 2022
Amir, according to your screenshots, you do NOT have a timetable. You have a table containg one datetime variable, and a separate double matrix. You need to make a timetable. One possibility might be
tt = timetable(t_avg.Time,load_avg)
another might be
tt = array2table(load_avg,'RowTimes',t_avg.Time)
but I have no idea what you are doing, so I can't really say how to make the timetable.

4 Comments

Hi Peter,
I want to have the same heatmap with only one difference: using datetime x-axis (21 Sep 12:30 to 18 Oct 00:00) instead of (numbers from 1 to100).
I have created this "kurt2" timetable but I don't know how I can plot the heatmap.
Thanks,
Amir
From doc heatmap: "heatmap(tbl,xvar,yvar) creates a heatmap from the table tbl. The xvar input indicates the table variable to display along the x-axis. The yvar input indicates the table variable to display along the y-axis."
You don't seem to have anything like that. The table syntax assumes raw data, your data are already aggregated. It looks like you want the syntax: heatmap(cdata) creates a heatmap from matrix cdata, which is what you had, and I think asking about timetables is a redd herring.
I don't think that syntax accepts datetimes for the x axis. If all you need is to label the axis, I suggest this:
CustomXLabels = string(t_avg.Time);
CustomXLabels(mod(XLabels,10) ~= 0) = " ";
h.XDisplayLabels = CustomXLabels;
Thanks Peter, it's working now. Just one thing, how can I only display the hour and minute and not the second?
help datetime/string

Sign in to comment.

More Answers (1)

In this case, you may be better off with something like imagesc or surf instead of heatmap. heatmap is designed for cases in which your x and y dimensions are categorical in nature. In your case, your x-axis is time, which is continuous (even if your data has been discretized). This will let you work with the datetime data more natively, rather than resorting to manually generating your tick labels.
I recommend something like this:
y = 1:size(load_avg,2);
surf(t_avg.Time, y, load_avg');
view(2)
axis tight
caxis([0 max(max(load_avg))])
colorbar
colormap turbo % turbo is more percetually uniform than jet, but otherwise very similar
xlabel('Time');
ylabel('Unit');
xtickformat('hh:mm')

Categories

Community Treasure Hunt

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

Start Hunting!