[MATLAB Basic Drawing No. 16] Drawing Heatmap (Heatmap)

Heatmap

The main function of the heat map is to visually display the differences in key research objects, and it is mostly used in the analysis of differences in economics and engineering.

The heatmap function creates a heatmap

grammar

h=heatmap(tbl,xvar,yvar)
h=heatmap(tbl,xvar,yvar,'ColorVariable',cvar)
h=heatmap(cdata)
h=heatmap(xvalues,yvalues,cdata)
h=heatmap(___,Name,Value)
h=heatmap(parent,___)
说明
h = heatmap(tbl,xvar,yvar) 基于表 tbl 创建一个热图,并返回 HeatmapChart 对象。xvar 输入参数指示沿 x 轴显示的表变量。yvar 输入参数指示沿 y 轴显示的表变量。默认颜色基于计数聚合,这种方法计算每对 x 和 y 值一起出现在表中的总次数。可使用 h 在创建热图之后对其进行修改。
h=heatmap(tbl,xvar,yvar,'ColorVariable',cvar) 使用 cvar 指定的表变量来计算颜色数据。默认的计算方法为均值聚合。
h=heatmap(cdata) 基于矩阵 cdata 创建一个热图。热图上的每个单元格对应 cdata 中的一个值。
h=heatmap(xvalues,yvalues,cdata) 指定沿 x 轴和 y 轴显示的值的标签。
h=heatmap(___,Name,Value) 使用一个或多个名称-值对组参数指定热图的其他选项。请在所有其他输入参数之后指定这些选项。
h = heatmap(parent,___) 在由 parent 指定的图窗、面板或选项卡上创建热图。

Case

Case 1: Basic drawing

This case uses the 'heatmap' command that comes with Matlab to draw a heat map. Although it looks good, there is a problem: its title, axis title, font size and other attributes cannot be set separately.

The picture is as follows:
Insert image description here
The MATLAB code is as follows:

clc
close all
clear
%% 基础设置
pathFigure= '.\Figures\' ;

figureUnits = 'centimeters';
figureWidth = 30; 
figureHeight = 20;

%% 绘制热图
figure(1) 
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]);
% 绘制热图:河东地区各站点干旱年内分布图
XName = ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"];
YName = ["景泰", "皋兰","靖远","榆中","临夏","临洮","华家岭","环县","平凉","西峰镇","玛曲","合作","岷县","武都","天水北道区"];
h = heatmap( XName , YName , SPI1Station ,'FontSize',10);
h.GridVisible = 'off';
% 设置坐标区名字与图的大标题
% h.Title = 'Tiltle';
h.XLabel = '月份';
h.YLabel = '站点';
% 对热图上色—colormap函数
hc = colormap(gca, flipud(hot) );
set(gca,'FontSize',16);   % ,'Fontname', 'Times New Roman'

str= strcat(pathFigure, "Fig.2", '.tiff');
print(gcf, '-dtiff', '-r600', str);

Case 2: Modify the properties of the heat map

The graph of the heat map includes attributes such as x-axis name, y-axis name, x and y data, color, data label, legend, etc. Its attributes can be set by modifying the handle of the return value of the heat map function.
The resulting picture is as follows: (Chinese font is Song Dynasty; Western font is Times New Roman)
Insert image description here
MATLAB code is as follows:

clc
close all
clear
%% 基础设置
pathFigure= '.\Figures\' ;

figureUnits = 'centimeters';
figureWidth = 30; 
figureHeight = 20;

figure(2) 
set(gcf, 'Units', figureUnits, 'Position', [0 0 figureWidth figureHeight]);
% 绘制热图:河东地区各站点干旱年内分布图
XName = ["1\fontname{宋体}月","2\fontname{宋体}月","3\fontname{宋体}月","4\fontname{宋体}月","5\fontname{宋体}月","6\fontname{宋体}月",...
    "7\fontname{宋体}月","8\fontname{宋体}月","9\fontname{宋体}月","10\fontname{宋体}月","11\fontname{宋体}月","12\fontname{宋体}月"];
YName = ["\fontname{宋体}景泰", "\fontname{宋体}皋兰","\fontname{宋体}靖远","\fontname{宋体}榆中","\fontname{宋体}临夏","\fontname{宋体}临洮",...
    "\fontname{宋体}华家岭","\fontname{宋体}环县","\fontname{宋体}平凉","\fontname{宋体}西峰镇","\fontname{宋体}玛曲","\fontname{宋体}合作","\fontname{宋体}岷县","\fontname{宋体}武都","\fontname{宋体}天水北道区"];
h = heatmap( XName , YName , SPI1Station ,'FontSize',10);
h.GridVisible = 'off';
h.FontName='Times New Roman';      % 设置显示的字体
h.FontSize = 14;                                 % 设置显示的字体大小
% 设置坐标区名字与图的大标题
% h.Title = 'Tiltle';
h.XLabel = '\fontname{
    
    宋体}月份';
h.YLabel = '\fontname{
    
    宋体}站点';
% 对热图上色—colormap函数
hc = colormap(gca, flipud(hot) );
set(gca,'FontSize',16);   % ,'Fontname', 'Times New Roman'

str= strcat(pathFigure, "Fig.2", '.tiff');
print(gcf, '-dtiff', '-r600', str);

reference

Guess you like

Origin blog.csdn.net/qq_44246618/article/details/131097572