主要内容

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温度, which contains average daily temperatures from January 2015 through July 2016. Read the file into a table and display the first five rows.

tbl =可读取('wempaturedata.csv');头(TBL,5)
ans=5×4 tableYear Month Day TemperatureF ____ ___________ ___ ____________ 2015 {'January'} 1 23 2015 {'January'} 2 31 2015 {'January'} 3 25 2015 {'January'} 4 39 2015 {'January'} 5 29

创建基本的热图

Create a heatmap that shows the months along thex-axis and years along they-axis. Color the heatmap cells using the temperature data by setting theColorVariableproperty. Assign the热图object to the variableh. Usehto modify the chart after it is created.

h =热图(tbl,'Month',“年”,'ColorVariable','TemperatureF');

图包含一个类型热图的对象。热图类型的图表具有vetiverf的标题平均值。

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

Reorder Values Along Axis

沿轴的值以字母顺序出现。重新排序几个月,以便它们按时间顺序出现。您可以使用分类数组或设置来自定义标签热图特性。

To use categorical arrays, first change the data in theMonthcolumn of the table from a cell array to a categorical array. Then use thereordercatsfunction to reorder the categories. You can apply these functions to the table in the workspace (TBL) or to the table stored in the可饮用属性热图object (h.SourceTable)。将它们应用于存储在热图object avoids affecting the original data.

h.sourcetable.month =分类(H.Sourcetable.month);neworder = {'January','February','March','April','May','June','七月',...'August','九月','October','十一月','December'}; h.SourceTable.Month = reordercats(h.SourceTable.Month,neworder);

图包含一个类型热图的对象。热图类型的图表具有vetiverf的标题平均值。

Similarly, you can add, remove, or rename the heatmap labels using theaddcats,removecats, orrenamecatsfunctions for categorical arrays.

Alternatively, you can reorder the values along an axis using theXDisplayDataYDisplayDataproperties of the热图目的。

h.xdisplaydata = {'January','February','March','April','May','June',...'七月','August','九月','October','十一月','December'};

图包含一个类型热图的对象。热图类型的图表具有vetiverf的标题平均值。

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 theTitle,XLABEL, 和YLabelproperties of the热图目的。For example, change the title and remove thex-axis label. Also, change the font size.

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

图包含一个类型热图的对象。The chart of type heatmap has title Average Temperatures.

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 theMissingDataColor缺少降落的特性。

H.MissingDataColor = [0.8 0.8 0.8];H.MissingDatalabel ='No Data';

图包含一个类型热图的对象。The chart of type heatmap has title Average Temperatures.

删除Colorbar

Remove the colorbar by setting theColorbarVisibleproperty.

H.ColorBarvisible ='离开';

图包含一个类型热图的对象。The chart of type heatmap has title Average Temperatures.

Format Cell Text

通过设置每个单元格中出现的文本格式CellLabelFormatproperty. For example, display the text with no decimal values.

h.CellLabelFormat ='%.0f';

图包含一个类型热图的对象。The chart of type heatmap has title Average Temperatures.

Add or Remove Values Along Axis

Show only the first month of each quarter by setting theXDisplayDataproperty. Add the year 2017 along they- 通过设置YDisplayDataproperty. Set these properties to a subset, superset, or permutation of the values inXData或者YData, respectively.

h.xdisplaydata = {'January','April','七月','October'}; h.YDisplayData = {'2015','2016','2017'};

图包含一个类型热图的对象。The chart of type heatmap has title Average Temperatures.

由于没有与2017年相关的数据,因此热图单元使用缺少的数据颜色。

See Also

Functions

Properties