MATLAB Answers

How to plot a heat map with three vectors?

43 views (last 30 days)
Rita on 30 May 2018
Commented: Rita on 1 Jun 2018
I have 3 vectors of hour of day , day of a year and temperature data . The temperature data measured 4 times a day. and for 365 days. I would like to plot a heat map like this


Sign in to comment.

Accepted Answer

Akbar on 1 Jun 2018
Edited: Akbar on 1 Jun 2018
I have made some modifications. The heatmap below Shows highest temp. each hour of a month, for two years.
% creating table (i hope you are using new Version of matlab
% which has table function, otherwise try dataset)
t1 = datetime(2016,01,1,8,0,0);
t2 = datetime(2018,01,1,8,0,0);
t = t1:hours(1):t2;
myTable = table(t',year(t'),month(t'),day(t'),hour(t'));
myTable.Properties.VariableNames = {'date' 'year' 'month' 'day' 'hour'};
a = 0;
b = 40;
r = (b-a).*rand(17545,1) + a;
myTable.temp = r;
% Create categorical arrays from the month
% and hour columns of the table.
% Then determine the unique months and hours
% to use as labels along the x-axis and y-axis.
months = categorical(myTable.month);
hours = categorical(myTable.hour);
xlabels = categories(months);
ylabels = categories(hours);
%Determine the final size of the resulting
%color data based on the number of unique months and hours.
nummonths = numel(xlabels);
numhours = numel(ylabels);
%Convert the categorical months and hours arrays
%into numeric indices to use with the accumarray function.
%Compute the color data as the maximum temperature for each
%month and hour combination using the accumarray function.
%Use NaN for missing month and year combinations.
x = double(months);
y = double(hours);
temps = myTable.temp;
cdata = accumarray([y,x],temps,[numhours,nummonths],@max,NaN);
%Create the heatmap. Label the x-axis and y-axis with the
%months and years, respectively. Color the heatmap cells
%using the computed matrix data.
h = heatmap(xlabels,ylabels,cdata);

More Answers (1)

Ameer Hamza
Ameer Hamza on 30 May 2018
You can make such a graph using pcolor(). The actual procedure depends on the format your data is available. You might need to use findgroups() and splitapply first to separate your data and then use pcolor() to plot the graph.

  1 Comment

Rita on 30 May 2018
Thanks Ameer.I don't think pcolor can plot what I wanted. I would like to use "heatmap" and I also would like to show nans as well.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!