从表格数据中创建热图
热图是使用颜色可视化数据的一种方式。此示例显示了如何将文件导入MATLAB®作为表格并从表列创建热图。它还显示了如何修改热图的外观,例如设置标题和轴标签。
导入文件作为表
加载示例文件TemperatureData.csv
,其中包含从2015年1月到2016年7月的平均每日温度。将文件读取到表格并显示前五行。
tbl = readtable('TemperatureData.csv');Head(tbl,5)
ans =5×4桌年度每月温度f ____ ___________ ___ ____________ 2015 {'1月'} 1 23 2015 {'1月'} 2 31 2015 {'1月'} 3 25 2015 2015 {'1月'} 4 39 2015 {'1月'} 5 29
Create Basic Heatmap
创建一个热图,显示沿着X- 轴和多年y-轴。通过设置温度数据为热图单元着色colorvaria
财产。分配HeatmapChart
对象变量H
。利用H
创建图表后修改。
H= heatmap(tbl,'月',,,,'年',,,,“ colorvariable',,,,'温度);
默认情况下,MATLAB将颜色数据计算为每个月的平均温度。但是,您可以通过设置菌落
财产。
沿轴的重新排序值
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 or by settingHeatmapChart
properties.
要使用分类阵列,请首先更改数据月
表从单元格数组到分类数组的列。然后使用重新订购
重新排序类别的功能。您可以将这些功能应用于工作空间中的表(tbl
)或存储在SourceTable
property of theHeatmapChart
目的 (H.Sourcetable
). Applying them to the table stored in theHeatmapChart
对象避免影响原始数据。
H.Sourcetable。月= categorical(h.SourceTable.Month); neworder = {'一月',,,,'二月',,,,'行进',,,,'四月',,,,'可能',,,,'六月',,,,'July',,,,...'八月',,,,'September',,,,'十月',,,,'November',,,,'十二月'};h.sourcetable.month = reordercats(h.sourcetable.month,neworder);
同样,您可以使用该标签添加,删除或重命名Heatmap标签addcats
,,,,拆卸
, 或者Renamecats
分类阵列的功能。
另外,您可以使用XDISPLAYDATA
andYDISPLAYDATA
属性HeatmapChart
目的。
H。XDISPLAYDATA= {'一月',,,,'二月',,,,'行进',,,,'四月',,,,'可能',,,,'六月',,,,...'July',,,,'八月',,,,'September',,,,'十月',,,,'November',,,,'十二月'};
修改标题和轴标签
当您使用表格数据创建热图时,热图会自动生成标题和轴标签。通过设置标题和轴标签标题
,,,,XLabel
,,,,andylabel
属性HeatmapChart
目的。例如,更改标题并删除X- 轴标签。另外,更改字体大小。
h.title =“平均温度”;H。XLabel ='';h.fontsize = 12;
修改缺失数据单元的外观
由于没有2016年8月至2016年12月的数据,因此这些细胞似乎是丢失的数据。使用丢失的数据单元格的外观失踪
andMissingDataLabel
properties.
H。失踪= [0.8 0.8 0.8]; h.MissingDataLabel ='没有数据';
卸下配色栏
通过设置ColorBarvissible
财产。
H。ColorBarvissible=“关闭”;
格式单元文本
Customize the format of the text that appears in each cell by setting theCelllabelformat
财产。例如,显示没有小数值的文本。
H.celllabelformat ='%.0f';
沿轴添加或删除值
仅通过设置每个季度的第一个月XDISPLAYDATA
财产。添加2017年y-axis by setting theYDISPLAYDATA
财产。将这些属性设置为值的子集,超集或排列XDATA
orYDATA
, 分别。
H。XDISPLAYDATA= {'一月',,,,'四月',,,,'July',,,,'十月'};h.ydisplaydata = {'2015',,,,'2016',,,,'2017'};
Since there is no data associated with the year 2017, the heatmap cells use the missing data color.