This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Create Heatmap from Tabular Data

Heatmaps are a way to visualize data using color. This example shows how to import a file into MATLAB® as a table and create a heatmap from the table columns. It also shows how to modify the appearance of the heatmap, such as setting the title and axis labels.

Import File as Table

Load the sample file TemperatureData.csv, which contains average daily temperatures from January 2015 through July 2016. Read the file into a table and display the first five rows.

tbl = readtable(fullfile(matlabroot,'examples','graphics','TemperatureData.csv'));
ans =

  5×4 table

    Year      Month      Day    TemperatureF
    ____    _________    ___    ____________

    2015    'January'    1      23          
    2015    'January'    2      31          
    2015    'January'    3      25          
    2015    'January'    4      39          
    2015    'January'    5      29          

Create Basic Heatmap

Create a heatmap that shows the months along the x-axis and years along the y-axis. Color the heatmap cells using the temperature data by setting the ColorVariable property. Assign the HeatmapChart object to the variable h. Use h to modify the chart after it is created.

h = heatmap(tbl,'Month','Year','ColorVariable','TemperatureF');

By default, MATLAB calculates the color data as the average temperature for each month. However, you can change the calculation method by setting the ColorMethod property.

Reorder Values Along Axis

The values along an axis appear in alphabetical order. Reorder the months so that they appear in chronological order. You can customize the labels using categorical arrays.

First change the data in the Month column of the table from a cell array to a categorical array using the categorical function. Then use the reordercats function to reorder the categories.

h.SourceTable.Month = categorical(h.SourceTable.Month);
neworder = {'January','February','March','April','May','June','July',...
h.SourceTable.Month = reordercats(h.SourceTable.Month,neworder);

Similarly, you can add, remove, or rename the heatmap labels using the addcats, removecats, or renamecats functions for categorical arrays. View the current labels using the categories function.

You can apply these functions to the table in the workspace (tbl). Alternatively, you can apply them to the table stored in the SourceTable property of the HeatmapChart object (h.SourceTable). Applying them to the table stored in the HeatmapChart object avoids affecting the original data.

Modify Title and Axis Labels

When you create a heatmap using tabular data, the heatmap automatically generates a title and axis labels. Customize the title and axis labels by setting the Title, XLabel, and YLabel properties of the HeatmapChart object. For example, change the title and remove the x-axis label. Also, change the font size.

h.Title = 'Average Temperatures';
h.XLabel = '';
h.FontSize = 12;

Modify Appearance of Missing Data Cells

Since there is no data for August 2016 through December 2016, those cells appear as missing data. Modify the appearance of the missing data cells using the MissingDataColor and MissingDataLabel properties.

h.MissingDataColor = [0.8 0.8 0.8];
h.MissingDataLabel = 'No Data';

Remove Colorbar

Remove the colorbar by setting the ColorbarVisible property.

h.ColorbarVisible = 'off';

Format Cell Text

Customize the format of the text that appears in each cell by setting the CellLabelFormat property. For example, display the text with no decimal values.

h.CellLabelFormat = '%.0f';

Add or Remove Values Along Axis

Show only the first month of each quarter. Use the removecats function to remove the unwanted categories. The removecats function replaces the removed months with undefined in the table stored in the SourceTable property.

rmv = {'February','March','May','June','August','September',...
h.SourceTable.Month = removecats(h.SourceTable.Month,rmv);

Add the year 2017 along the y-axis. First, convert the data in the Year column from a cell array to a categorical array. Use the addcats function to add the additional category. Since there is no data associated with this year, the heatmap cells use the missing data color.

h.SourceTable.Year = categorical(h.SourceTable.Year);
h.SourceTable.Year = addcats(h.SourceTable.Year,'2017');

See Also



Was this topic helpful?