好早之前写过一个绘制相关系数矩阵的代码,但是会自动求相关系数,而且画出来的热图只能是方形,这里写一款允许nan值出现,任意形状的热图绘制代码,绘制效果如下:
![](https://img-blog.csdnimg.cn/img_convert/791e48e07d73d0ae64b0738e9025441f.png)
![](https://img-blog.csdnimg.cn/img_convert/fd6724933ad99cce5353ece6f17b7d19.png)
![](https://img-blog.csdnimg.cn/img_convert/1f92978093785dbdcee6caa1b2444dd2.png)
![](https://img-blog.csdnimg.cn/img_convert/3f49a0260178b9c01bec020ebdbb8235.png)
如遇到bug请后台提出,并去gitee下载更新版本
教程部分
1 基础绘制
假设有个随机数矩阵,绘图:
Data=rand(15,15);
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
![](https://img-blog.csdnimg.cn/img_convert/f7ff1a304d3e37fb78dd787dca8018d6.png)
假设存在负数:
Data=rand(15,15)-.5;
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
![](https://img-blog.csdnimg.cn/img_convert/bae3505ff08e7f4961ab622967a5c7cb.png)
假设存在nan:
Data=rand(12,12)-.5;
Data([4,5,13])=nan;
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
![](https://img-blog.csdnimg.cn/img_convert/cdc05c389cb1f25eb91c24cca9026345.png)
假设非方阵:
Data=rand(25,30);
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
![](https://img-blog.csdnimg.cn/img_convert/5f54e413bf5f4cefe50dc2db73a30611.png)
颜色怎么换,在后面会讲到。
2 显示文字
使用setText方法显示文字:
Data=rand(12,12)-.5;
Data([4,5,13])=nan;
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
SHM.setText();
![](https://img-blog.csdnimg.cn/img_convert/f50e980c1d85792d08ccca8dcd8c54c1.png)
如果要修饰或显示文字请在修改颜色后进行。
3 修改形状
形状有以下几个可选项:
sq : 方形(默认)pie : 饼图circ : 圆形oval : 椭圆形hex : 六边形asq : 自带调整大小的方形acirc : 自带调整大小的圆形
figure()
Data=rand(12,12)-.5;
SHM=SHeatmap(Data,'Format','oval');
SHM=SHM.draw();
figure()
Data=rand(12,12)-.5;
SHM=SHeatmap(Data,'Format','pie');
SHM=SHM.draw();
![](https://img-blog.csdnimg.cn/img_convert/4bd7365f8d690580c0b0cd7ce153c9f3.png)
![](https://img-blog.csdnimg.cn/img_convert/bb5527fa48c7d9e21586b7ad38efcfc0.png)
展示一下全部类型:
sq
![](https://img-blog.csdnimg.cn/img_convert/49d436157c2ec592d36bfb387dcc81bc.png)
![](https://img-blog.csdnimg.cn/img_convert/922f12ac514ddafe28347ad20d245e4b.png)
pie
![](https://img-blog.csdnimg.cn/img_convert/9add2c78219bd837ba580558df657c55.png)
![](https://img-blog.csdnimg.cn/img_convert/7132166e22adcfc455ed33ac99dbfea1.png)
circ
![](https://img-blog.csdnimg.cn/img_convert/55a84cea8f4bb072fb55f481a7cacf41.png)
![](https://img-blog.csdnimg.cn/img_convert/e4edc8e9e7f0b66fa4ba59aba27f0e37.png)
oval
![](https://img-blog.csdnimg.cn/img_convert/35bffe4140fdb53c0d50d9ccba0c1a55.png)
![](https://img-blog.csdnimg.cn/img_convert/6cdd1b696cf09225a87bdac43037b040.png)
hex
![](https://img-blog.csdnimg.cn/img_convert/3323229e5ab717677b22fafe01e60bd0.png)
![](https://img-blog.csdnimg.cn/img_convert/4a7fc1a67aefc8ce56e6e34cf88fb2fe.png)
asq
![](https://img-blog.csdnimg.cn/img_convert/798fe6fb92398007b24b9617c7ddb223.png)
![](https://img-blog.csdnimg.cn/img_convert/1772e82359582eb3581ae7c81a40c9d3.png)
acirc
![](https://img-blog.csdnimg.cn/img_convert/eb248dd007be2a502e3926849f0f8a4a.png)
![](https://img-blog.csdnimg.cn/img_convert/a068ea65db77636b9cec43533ee9a88e.png)
4 颜色范围
新版本使用clim老版本使用caxis调整映射范围:
Data=rand(12,12)-.5;
Data([4,5,13])=nan;
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
clim([-.8,.8])
SHM.setText();
调整前:
![](https://img-blog.csdnimg.cn/img_convert/346f1835210e49470880774811a8899b.png)
调整后:
![](https://img-blog.csdnimg.cn/img_convert/c23182b80688a27b2198855cc973f1ba.png)
5 colormap
matlab自带的colormap都能用:
Data=rand(14,14);
SHM=SHeatmap(Data,'Format','sq');
SHM.draw();
colormap(bone)
![](https://img-blog.csdnimg.cn/img_convert/1c935510feed32c2b136a6a03e4650c3.png)
可以联合我写的slanCM工具函数: https://slandarer.blog.csdn.net/article/details/127719784
![](https://img-blog.csdnimg.cn/img_convert/6185f6bcda6a6a0994a273471461a09d.png)
举点例子:
colormap(flipud(slanCM(20)))
![](https://img-blog.csdnimg.cn/img_convert/a3a621c734484ba76b80254eaaeef384.png)
colormap(flipud(slanCM(21)))
![](https://img-blog.csdnimg.cn/img_convert/15a433c644499f293616b49a62bc14a7.png)
colormap(slanCM(61))
![](https://img-blog.csdnimg.cn/img_convert/0e941a942c4379c7cdeff51696ab8c9e.png)
colormap(slanCM(177))
![](https://img-blog.csdnimg.cn/img_convert/564e00e339904f63d4b5f8355649deed.png)
再比如:
Data=rand(12,12)-.5;
Data([4,5,13])=nan;
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
clim([-.7,.7])
colormap(slanCM(141))
SHM.setText();
![](https://img-blog.csdnimg.cn/img_convert/300b867c2ba536da803e6d7949c2bc85.png)
clim([-.7,.7])
colormap(slanCM(136))
![](https://img-blog.csdnimg.cn/img_convert/cce521055a35201db68bec7de2293ebd.png)
clim([-.7,.7])
colormap(slanCM(134))
![](https://img-blog.csdnimg.cn/img_convert/9f3e5a8ba262824c27dfa3aeb5934e28.png)
clim([-.8,.8])
colormap(slanCM(110))
![](https://img-blog.csdnimg.cn/img_convert/c76124fceabd1e51df904041309df88e.png)
clim([-1,1])
colormap(slanCM(97))
![](https://img-blog.csdnimg.cn/img_convert/9f8637f641a0096edb44c1e9202f9d2d.png)
6 调整colormap位置
假设编写如下代码:
Data=rand(3,12);
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
改成这样:
Data=rand(3,12);
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
CB=colorbar;
CB.Location='southoutside';
![](https://img-blog.csdnimg.cn/img_convert/eab395b5d337eb515292028f64f56988.png)
7 修改标签
改编x轴y轴标签就还是设置XTickLabel 和YTickLabel:
Data=rand(10,10);
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
ax=gca;
ax.XTickLabel={'X-1','X-2','X-3','X-4','X-5','X-6','X-7','X-8','X-9','X-10'};
ax.YTickLabel={'Y-1','Y-2','Y-3','Y-4','Y-5','Y-6','Y-7','Y-8','Y-9','Y-10'};
ax.FontSize=14;
![](https://img-blog.csdnimg.cn/img_convert/99804ab2934119a8a01672703762ba61.png)
8 整体修饰
使用:
setBox :修饰边框setPatch :修饰图形setText :修饰文本
可对各个部分进行整体修改。
修改图形边缘及框颜色:
Data=rand(10,10);
Data([4,5,13])=nan;
SHM=SHeatmap(Data,'Format','pie');
SHM=SHM.draw();
SHM.setBox('Color',[0,0,.8])
SHM.setPatch('EdgeColor',[.8,0,0])
![](https://img-blog.csdnimg.cn/img_convert/41b40586acebc955bf2938ebf3895ab7.png)
修改全部文本颜色:
Data=rand(10,10);
Data([4,5,13])=nan;
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
SHM.setText('Color',[0,0,.8],'FontSize',14)
![](https://img-blog.csdnimg.cn/img_convert/ee7c2474174383e757f4eb037f4e0c4a.png)
9 局部修改
使用:
setTextMNsetPatchMN
设置第m行n列对象的属性:
举个例子,把数值大于0.9部分文字变为星号,并用红色框框起来:
Data=rand(9,9);
Data([4,5,13])=nan;
SHM=SHeatmap(Data,'Format','sq');
SHM=SHM.draw();
SHM.setText();
for i=1:size(Data,1)
for j=1:size(Data,2)
if Data(i,j)>.9
SHM.setTextMN(i,j,'String','**','FontSize',20)
SHM.setPatchMN(i,j,'EdgeColor',[1,0,0],'LineWidth',2)
end
end
end
![](https://img-blog.csdnimg.cn/img_convert/49e8e442d1a15350a44fac0f9acc2271.png)
局部设置可设置nan部分颜色:
SHM.setPatchMN(4,1,'FaceColor',[.8,.6,.6])
![](https://img-blog.csdnimg.cn/img_convert/d7fe791e18b79b8d18503383026f36ea.png)
工具函数完整代码
classdef SHeatmap
% =========================================================================
% Format
% -------------------------------------------------------------------------
% sq : 方形(默认)
% pie : 饼图
% circ : 圆形
% oval : 椭圆形
% hex : 六边形
% asq : 自带调整大小的方形
% acirc : 自带调整大小的圆形
% =========================================================================
% @author : slandarer
% gzh : slandarer随笔
% -------------------------------------------------------------------------
% Zhaoxu Liu / slandarer (2023). special heatmap
% (https://www.mathworks.com/matlabcentral/fileexchange/125520-special-heatmap),
% MATLAB Central File Exchange. 检索来源 2023/3/1.
% -------------------------------------------------------------------------
properties
ax,arginList={'Format'}
Format='sq' % sq : 方形(默认)
% pie : 饼图
% circ : 圆形
% oval : 椭圆形
% hex : 六边形
% asq : 自带调整大小的方形
% acirc : 自带调整大小的圆形
Data
dfColor1=[0.9686 0.9882 0.9412; 0.9454 0.9791 0.9199; 0.9221 0.9700 0.8987; 0.8988 0.9609 0.8774;
0.8759 0.9519 0.8560; 0.8557 0.9438 0.8338; 0.8354 0.9357 0.8115; 0.8152 0.9276 0.7892;
0.7909 0.9180 0.7685; 0.7545 0.9039 0.7523; 0.7180 0.8897 0.7361; 0.6816 0.8755 0.7199;
0.6417 0.8602 0.7155; 0.5962 0.8430 0.7307; 0.5507 0.8258 0.7459; 0.5051 0.8086 0.7610;
0.4596 0.7873 0.7762; 0.4140 0.7620 0.7914; 0.3685 0.7367 0.8066; 0.3230 0.7114 0.8218;
0.2837 0.6773 0.8142; 0.2483 0.6378 0.7929; 0.2129 0.5984 0.7717; 0.1775 0.5589 0.7504;
0.1421 0.5217 0.7314; 0.1066 0.4853 0.7132; 0.0712 0.4488 0.6950; 0.0358 0.4124 0.6768;
0.0314 0.3724 0.6364; 0.0314 0.3319 0.5929; 0.0314 0.2915 0.5494; 0.0314 0.2510 0.5059]
dfColor2=[0.6196 0.0039 0.2588; 0.6892 0.0811 0.2753; 0.7588 0.1583 0.2917; 0.8283 0.2354 0.3082;
0.8706 0.2966 0.2961; 0.9098 0.3561 0.2810; 0.9490 0.4156 0.2658; 0.9660 0.4932 0.2931;
0.9774 0.5755 0.3311; 0.9887 0.6577 0.3690; 0.9930 0.7266 0.4176; 0.9943 0.7899 0.4707;
0.9956 0.8531 0.5238; 0.9968 0.9020 0.5846; 0.9981 0.9412 0.6503; 0.9994 0.9804 0.7161;
0.9842 0.9937 0.7244; 0.9526 0.9810 0.6750; 0.9209 0.9684 0.6257; 0.8721 0.9486 0.6022;
0.7975 0.9183 0.6173; 0.7228 0.8879 0.6325; 0.6444 0.8564 0.6435; 0.5571 0.8223 0.6448;
0.4698 0.7881 0.6460; 0.3868 0.7461 0.6531; 0.3211 0.6727 0.6835; 0.2553 0.5994 0.7139;
0.2016 0.5261 0.7378; 0.2573 0.4540 0.7036; 0.3130 0.3819 0.6694; 0.3686 0.3098 0.6353]
Colormap;maxV
patchHdl;boxHdl;pieHdl;textHdl
end
methods
function obj=SHeatmap(Data,varargin)
obj.Data=Data;
obj.maxV=max(max(abs(obj.Data)));
% 获取其他数据
disp(char([64 97 117 116 104 111 114 32 58 32,...
115 108 97 110 100 97 114 101 114]))
for i=1:2:(length(varargin)-1)
tid=ismember(obj.arginList,varargin{i});
if any(tid)
obj.(obj.arginList{tid})=varargin{i+1};
end
end
% 设置配色
if any(any(obj.Data:})
end
% 设置图形样式
function setPatch(obj,varargin)
for row=1:size(obj.Data,1)
for col=1:size(obj.Data,2)
if ~isnan(obj.Data(row,col))
set(obj.patchHdl(row,col),varargin{:})
if isequal(obj.Format,'pie')
set(obj.pieHdl(row,col),varargin{:})
end
end
end
end
end
function setPatchMN(obj,m,n,varargin)
set(obj.patchHdl(m,n),varargin{:})
if isequal(obj.Format,'pie')
set(obj.pieHdl(m,n),varargin{:})
end
end
% 设置框样式
function setBox(obj,varargin)
set(obj.boxHdl,varargin{:})
end
end
% =========================================================================
% @author : slandarer
% gzh : slandarer随笔
% -------------------------------------------------------------------------
% Zhaoxu Liu / slandarer (2023). special heatmap
% (https://www.mathworks.com/matlabcentral/fileexchange/125520-special-heatmap),
% MATLAB Central File Exchange. 检索来源 2023/3/1.
% -------------------------------------------------------------------------
end
完
编程不易,希望大家该点赞的点赞,该在看的在看!!
未经允许本代码请勿作商业用途,引用的话可以引用我file exchange上的链接,可使用如下格式:
Zhaoxu Liu / slandarer (2023). special heatmap (https://www.mathworks.com/matlabcentral/fileexchange/125520-special-heatmap), MATLAB Central File Exchange. 检索来源 2023/3/1.
若转载请保留以上file exchange链接及本文链接!!!!!
|