function [hImage, hText, hXText] = heatmap(mat, xlab, ylab, textmat, varargin)
% HEATMAP displays a matrix as a heatmap image
%
% USAGE:
% [hImage, hText, hTick] = heatmap(matrix, xlabels, ylabels, textmatrix, 'param', value, ...)
%
% INPUTS:
% * HEATMAP displays "matrix" as an image whose color intensities reflect
% the magnitude of the values in "matrix".
%
% * "xlabels" (and "ylabels") can be either a numeric vector or cell array
% of strings that represent the columns (or rows) of the matrix. If either
% is not specified or empty, no labels will be drawn.
%
% * "textmat" can either be: 1 (or true), in which case the "matrix" values will be
% displayed in each square, a format string, in which case the matrix
% values will be displayed formatted according to the string specified, a numeric
% matrix the size of "matrix", in which case those values will be displayed as
% strings or a cell matrix of strings the size of "matrix", in which case each
% string will be displayed. If not specified or empty, no text will be
% displayed on the image
%
% OTHER PARAMETERS (passed as parameter-value pairs)
% * 'Colormap': Either a matrix of size numLevels-by-3 representing the
% colormap to be used or a string or function handle representing a
% function that returns a colormap, example, 'jet', 'hsv' or @cool.
% Non-standard colormaps available within HEATMAP include 'money' and 'red'.
% By default, the current figure's colormap is used.
%
% * 'ColorLevels': The number of distinct levels in the colormap (default:
% 64). If more levels are specified than are present in the colormap, the
% levels in the colormap are interpolated. If fewer are specified the
% colormap is downsampled.
%
% * 'UseLogColormap': A true/false value which, if true, specifies that the
% intensities displayed should match the log of the "matrix" values. Use
% this if the data is naturally on a logarithmic scale (default: false)
%
% * 'UseFigureColormap': Specifies whether the figure's colormap should be
% used. If false, the color intensities after applying the
% specified/default colormap will be hardcoded, so that the image will be
% independent of the figure's colormap. If this option is true, the figure
% colormap in the end will be replaced by specified/default colormap.
% (default = true)
%
% * 'NaNColor': A 3-element [R G B] vector specifying the color used to display NaN
% or missing value. [0 0 0] corresponds to black and [1 1 1] to white. By
% default MATLAB displays NaN values using the color assigned to the
% lowest value in the colormap. Specifying this option automatically sets
% the 'UseFigureColormap' option to false because the color mapping must
% be computed prior to setting the nan color.
%
% * 'MinColorValue': A scalar number corresponding to the value of the data
% that is mapped to the lowest color of the colormap. By default this is
% the minimum value of the matrix input.
%
% * 'MaxColorValue': A scalar number corresponding to the value of the data
% that is mapped to the highest color of the colormap. By default this is
% the maximum value of the matrix input.
%
% * 'Parent': Handle to an axes object
%
% * 'TextColor': Either a color specification of all the text displayed on
% the image or a string 'xor' which sets the EraseMode property of the text
% objects to 'xor'. This will display all the text labels in a color that
% contrasts its background.
%
% * 'FontSize': The initial fontSize of the text labels on the image. As
% the image size is scaled the fontSize is shrunk appropriately.
%
% * 'ColorBar': Display colorbar. The corresponding value parameter should
% be either logical 1 or 0 or a cell array of any additional parameters
% you wish to pass to the colorbar function (such as location)
%
% * 'GridLines': Draw grid lines separating adjacent sections of the
% heatmap. The value of the parameter is a LineStyle specification, for example,
% :, -, -. or --. By default, no grid lines are drawn.
%
% * 'TickAngle': Angle of rotation of tick labels on x-axis. (Default: 0)
%
% * 'ShowAllTicks': Set to 1 or true to force all ticks and labels to be
% drawn. This can make the axes labels look crowded. (Default: false)
%
% * 'TickFontSize': Font size of the X and Y tick labels. Default value is
% the default axes font size, usually 10. Set to a lower value if many
% tick labels are being displayed
%
% * 'TickTexInterpreter': Set to 1 or true to render tick labels using a TEX
% interpreter. For example, '_b' and '^o' would be rendered as subscript
% b and the degree symbol with the TEX interpreter. This parameter is only
% available in MATLAB R2014b and above (Default: false)
%
% OUTPUTS:
% * hImage: handle to the image object
% * hText : handle to the text objects (empty if no text labels are drawn)
% * hTick : handle to the X-tick label text objects if tick angle is not 0
% (empty otherwise)
%
% Notes:
% * The 'money' colormap displays a colormap where 0 values are mapped to
% white, negative values displayed in varying shades of red and positive
% values in varying shades of green
% * The 'red' colormap maps 0 values to white and higher values to red
%
% EXAMPLES:
% data = reshape(sort(randi(100,10)),10,10)-50;
% heatmap(data, cellstr(('A':'J')'), mean(data,2), '%0.0f%%',...
% 'Colormap', 'money', 'Colorbar', true, 'GridLines', ':',...
% 'TextColor', 'b')
% For detailed examples, see the associated document heatmap_examples.m
% Copyright The MathWorks, Inc. 2009-2014
% Handle missing inputs
if nargin < 1, error('Heatmap requires at least one input argument'); end
if nargin < 2, xlab = []; end
if nargin < 3, ylab = []; end
if nargin < 4, textmat = []; end
% Parse parameter/value inputs
p = parseInputs(mat, varargin{:});
% Get heatmap axes information if it already exists
p.axesInfo = getHeatmapAxesInfo(p.hAxes);
% Calculate the colormap based on inputs
p = calculateColormap(p, mat);
% Create heatmap image
p = plotHeatmap(p, mat); % New properties hImage and cdata added
% Generate grid lines if selected
generateGridLines(p);
% Set axes labels
[p, xlab, ylab, hXText, origPos] = setAxesTickLabels(p, xlab, ylab);
% Set text labels
[p, displayText, fontScaleFactor] = setTextLabels(p, mat, textmat);
% Add colorbar if selected
addColorbar(p, mat, textmat)
% Store heatmap properties in axes for callbacks
axesInfo = struct('Type', 'heatmap', 'Parameters', p, 'FontScaleFactor', ...
fontScaleFactor, 'mat', mat, 'hXText', hXText, ...
'origAxesPos', origPos);
axesInfo.xlab = xlab;
axesInfo.ylab = ylab;
axesInfo.displayText = displayText;
set(p.hAxes, 'UserData', axesInfo);
% Define callbacks
dObj = datacursormode(p.hFig);
set(dObj, 'Updatefcn', @cursorFun);
zObj = zoom(p.hFig);
set(zObj, 'ActionPostCallback', @(obj,evd)updateLabels(evd.Axes,true));
pObj = pan(p.hFig);
% set(pObj, 'ActionPreCallback', @prePan);
set(pObj, 'ActionPostCallback', @(obj,evd)updateLabels(evd.Axes,true));
set(p.hFig, 'ResizeFcn', @resize)
% Set outputs
hImage = p.hImage;
hText = p.hText;
end
% ---------------------- Heatmap Creation Functions ----------------------
% Parse PV inputs & return structure of parameters
function param = parseInputs(mat, varargin)
p = inputParser;
p.addParamValue('Colormap',[]); %#ok<*NVREPL>
p.addParamValue('ColorLevels',[]);
p.addParamValue('TextColor',[0 0 0]);
p.addParamValue('UseFigureColormap',true);
p.addParamValue('UseLogColormap',false);
p.addParamValue('Parent',NaN);
p.addParamValue('FontSize',[]);
p.addParamValue('Colorbar',[]);
p.addParamValue('GridLines','none');
p.addParamValue('TickAngle',0);
p.addParamValue('ShowAllTicks',false);
p.addParamValue('TickFontSize',[]);
p.addParamValue('TickTe
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【达摩老生出品,必属精品,亲测校正,质量保证】 资源名:heatmaps_matlab.rar 资源类型:matlab项目全套源码 源码说明: 全部项目源码都是经过测试校正后百分百成功运行的,如果您下载后不能运行可联系我进行指导或者更换。 适合人群:新手及有一定经验的开发人员
资源推荐
资源详情
资源评论
收起资源包目录
heatmaps_matlab.rar (2个子文件)
heatmaps
heatmapData.mat 14KB
heatmap.m 27KB
共 2 条
- 1
资源评论
- liHd2023-01-04资源很不错,内容和描述一致,值得借鉴,赶紧学起来!
- 鹿儿2023-04-19资源是宝藏资源,实用也是真的实用,感谢大佬分享~
- aaronwong20182023-06-18果断支持这个资源,资源解决了当前遇到的问题,给了新的灵感,感谢分享~
阿里matlab建模师
- 粉丝: 3269
- 资源: 2780
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功