MATLAB | 绘图复刻(二十二)| 带树状图的三角热图合集

悄悄说句题外话,MATLAB 官方入驻知乎啦,趁着还小快去欺负~

本期推送大概讲一下各种各样的三角热图咋画,众所周知,顶刊中很喜欢用这些花里胡哨的热图,例如:

绘图工具

这里使用我开发的树状图绘制工具,虽然在这个工具里各种角度各种旋转的热图只是附属工具,但还是够用了,使用教程及代码仓库请见:

当然我会在文末把需要用到的代码和示例放在一起,可以直接找文末链接。

正文

我就直接给模板代码了,注释做的还算比较清晰:大体就是走的创建热图->旋转->隐藏上半或者下半部分的方式来进行的实现。配色可以直接使用colormap函数修改。

涉及要画树状图或者要分组的情况,就需要先使用树状图绘制工具进行分组,然后把数据传入热图绘制工具:

matlab 复制代码
SM.RowOrder = ST.order;
SM.RowClass = ST.class;
SM.ColOrder = ST.order;
SM.ColClass = ST.class;

倒三角热图

matlab 复制代码
% 随机生成数据
X = randn(20,20)+[(linspace(-1,2.5,20)').*ones(1,8),(linspace(.5,-.7,20)').*ones(1,5),(linspace(.9,-.2,20)').*ones(1,7)];
Data = corr(X);
% 变量名列表
NameList = compose('Sl-%d',1:20);

fig = figure('Units', 'normalized', 'Position', [.05,.1,.6,.8], 'Color', 'w');
%% ========================================================================
% 创建热图对象 -- create heatmap object
SM = SMatrix(Data);
SM.ColName = NameList;
% 设置文本和字体 -- Set Text and Font
SM.LeftLabel = 'off';
SM.BottomLabel = 'off';
SM.TopLabel = 'on';
SM.TopLabelFont = {'FontSize', 15, 'FontName', 'Times New Roman', 'Rotation', 45};
% 设置位置 -- set position
SM.XLim = [0,1];
SM.YLim = [0,1];
SM.TLim = [-pi/4,-pi/4];
SM.draw()
%% ========================================================================
% 修饰句柄 -- decorate handles
% 清除热图下半部分 -- Clear the bottom half of the heatmap
tData = triu(ones(size(Data)),1);
tInd = find(tData(:) == 1);
for i = 1:length(tInd)
    set(SM.heatmapHdl{tInd(i)}, 'Visible', 'off')
end
% 修饰坐标区域 -- decorate axes
set(gca, 'XColor', 'none', 'YColor', 'none',...
    'DataAspectRatio', [1,1,1],'XLim', [0,1.45]);
CB = colorbar();
set(CB,'Location','southoutside','FontName','Times New Roman','FontSize',14);

带分组树状图倒三角热图

matlab 复制代码
% 随机生成数据
X=randn(20,20)+[(linspace(-1,2.5,20)').*ones(1,8),(linspace(.5,-.7,20)').*ones(1,5),(linspace(.9,-.2,20)').*ones(1,7)];
Data = corr(X);
% 变量名列表
NameList = compose('Sl-%d',1:20);
% 分类配色 -- Color schemes for each clust
CList = [0.1490    0.4039    0.4980
         0.3882    0.3608    0.4471
         0.5373    0.2157    0.3098
         0.7686    0.4353    0.2431];

fig = figure('Units', 'normalized', 'Position', [.05,.1,.6,.8], 'Color', 'w');
%% ========================================================================
% 创建聚类树状图对象 -- create tree(dendrogram) object
% 左侧聚类树状图 -- left Cluster Tree
Z1 = linkage(Data, 'average');
ST = STree(Z1, 'MaxClust', 3); % 聚类数量,如果大于4请给CList多设置几个颜色
ST.Orientation = 'left';
ST.XLim = [-.25,-.05];
ST.YLim = [0,1];
ST.TLim = [-pi/4,-pi/4];
ST.Label = 'off';
ST.BranchColor = 'on';
ST.BranchHighlight = 'on';
ST.ClassHighlight = 'on';
ST.RTick = [0,1,1.2,0];
ST.CData = CList;
% 每个类之间加入间隙 -- insert gap between each clust
ST.ClustGap = 'on';
ST.draw()
%% ========================================================================
% 创建热图对象 -- create heatmap object
SM = SMatrix(Data);
% 添加分组信息 -- Add grouping information
SM.ColName = NameList;
SM.RowOrder = ST.order;
SM.RowClass = ST.class;
SM.ColOrder = ST.order;
SM.ColClass = ST.class;
% 设置文本和字体 -- Set Text and Font
SM.LeftLabel = 'off';
SM.BottomLabel = 'off';
SM.TopLabel = 'on';
SM.TopLabelFont = {'FontSize', 15, 'FontName', 'Times New Roman', 'Rotation', 45};
% 每个类之间加入间隙 -- insert gap between each clust
SM.ClustGap = 'on';
% 设置位置 -- set position
SM.XLim = [0,1];
SM.YLim = [0,1];
SM.TLim = [-pi/4,-pi/4];
SM.draw()
%% ========================================================================
% 修饰句柄 -- decorate handles
% 清除热图下半部分 -- Clear the bottom half of the heatmap
tData = triu(ones(size(Data)),1);
tInd = find(tData(:) == 1);
for i = 1:length(tInd)
    set(SM.heatmapHdl{tInd(i)}, 'Visible', 'off')
end
% 修饰坐标区域 -- decorate axes
set(gca, 'XColor', 'none', 'YColor', 'none',...
    'DataAspectRatio', [1,1,1],'XLim', [-.15,1.45]);
CB = colorbar();
set(CB,'Location','southoutside','FontName','Times New Roman','FontSize',14);

带倾斜树状图的三角热图

matlab 复制代码
% 随机生成数据
X = randn(20,20)+[(linspace(-1,2.5,20)').*ones(1,8),(linspace(.5,-.7,20)').*ones(1,5),(linspace(.9,-.2,20)').*ones(1,7)];
Data = corr(X);
% 变量名列表
NameList = compose('Sl-%d',1:20);

fig = figure('Units', 'normalized', 'Position', [.05,.1,.6,.8], 'Color', 'w');
%% ========================================================================
% 创建聚类树状图对象 -- create tree(dendrogram) object
% 左侧聚类树状图 -- left Cluster Tree
Z1 = linkage(Data, 'average');
ST = STree(Z1, 'MaxClust', 3); % 聚类数量,如果大于4请给CList多设置几个颜色
ST.Orientation = 'left';
ST.XLim = [-.25,0];
ST.YLim = [0,sqrt(2)];
ST.TLim = [-pi/4,-pi/4];
ST.Label = 'off';
ST.draw()
%% ========================================================================
% 创建热图对象 -- create heatmap object
SM = SMatrix(Data);
% 添加分组信息 -- Add grouping information
SM.RowOrder = ST.order;
SM.ColOrder = ST.order;
SM.ColName = NameList; SM.ColName{ST.order(1)} = '';
SM.RowName = NameList; SM.RowName{ST.order(end)} = '';
% 设置文本和字体 -- Set Text and Font
SM.RightLabel = 'on';
SM.LeftLabel = 'off';
SM.RightLabelFont = {'FontSize', 15, 'FontName', 'Times New Roman'};
SM.BottomLabelFont = {'FontSize', 15, 'FontName', 'Times New Roman'};
% 设置位置 -- set position
SM.XLim = [0,1];
SM.YLim = [0,1];
SM.TLim = [0,0];
SM.draw()
%% ========================================================================
% 修饰句柄 -- decorate handles
% 清除热图左上部分 -- Clear the bottom half of the heatlmap
tData = tril(ones(size(Data)), 0);
tInd = find(tData(:) == 1);
for i = 1:length(tInd)
    set(SM.heatmapHdl{tInd(i)}, 'Visible', 'off')
end
% 修饰坐标区域 -- decorate axes
set(gca, 'XColor', 'none', 'YColor', 'none',...
    'DataAspectRatio', [1,1,1], 'YLim',get(gca,'YLim')-[.05,0]);
CB = colorbar();
set(CB,'Location','southoutside','FontName','Times New Roman','FontSize',14);

带高亮框的分组倒三角热图

matlab 复制代码
% 随机生成数据
X = randn(20,10)+[(linspace(-1,2.5,20)').*ones(1,6),(linspace(.5,-.7,20)').*ones(1,4)];
Data = corr(X);
% 变量名列表
NameList = compose('Sl-%d',1:10);

% 通过树状图工具进行分类和数值计算(毕竟这个热图工具是树状图工具的附属)
Z1 = linkage(Data, 'average');
ST = STree(Z1, 'MaxClust', 2); 
ST.draw(); close all

fig = figure('Units', 'normalized', 'Position', [.05,.1,.6,.8], 'Color', 'w');
%% ========================================================================
% 创建热图对象 -- create heatmap object
SM = SMatrix(Data);
% 添加分组信息 -- Add grouping information
SM.RowOrder = ST.order;
SM.RowClass = ST.class;
SM.RowName = NameList;
SM.ColOrder = ST.order;
SM.ColClass = ST.class;
SM.ColName = NameList;
% 设置文本和字体 -- Set Text and Font
SM.LeftLabel = 'off';
SM.BottomLabelFont = {'FontSize', 15, 'FontName', 'Times New Roman', ...
    'Rotation',45, 'HorizontalAlignment','right', 'VerticalAlignment','baseline'};
% 设置位置 -- set position
SM.XLim = [0,1];
SM.YLim = [0,1];
SM.TLim = [-pi/4,-pi/4];
SM.draw()

%% ========================================================================
% 修饰句柄 -- decorate handles
% 清除热图下半部分 -- Clear the bottom half of the heatmap
tData = triu(ones(size(Data)),1);
tInd = find(tData(:) == 1);
for i = 1:length(tInd)
    set(SM.heatmapHdl{tInd(i)}, 'Visible', 'off')
end
% 移动标签位置
txtHdl = findobj(gca,'Type','text');
for i = 1:length(txtHdl)
    txtHdl(i).Position(2) = txtHdl(1).Position(2);
    txtHdl(i).Position(1) = txtHdl(i).Position(1).*sqrt(2).*sqrt(2);
end
% 修饰坐标区域 -- decorate axes
set(gca, 'XColor', 'none', 'YColor', 'none',...
    'DataAspectRatio', [1,1,1],'XLim', [-.02,sqrt(2) + .02]);
CB = colorbar();
set(CB,'Location','southoutside','FontName','Times New Roman','FontSize',14);
%% 绘制分组黑线 ============================================================
numN = length(ST.class);
numA = sum(ST.class == ST.class(1));
numB = sum(ST.class == ST.class(end));
plot([0,sqrt(2)/2,sqrt(2)].*numA./numN, [0,sqrt(2)/2,0].*numA./numN, 'Color','k', 'LineWidth',4)
plot(sqrt(2) - [0,sqrt(2)/2,sqrt(2)].*numB./numN, [0,sqrt(2)/2,0].*numB./numN, 'Color','k', 'LineWidth',4)
xx = linspace(0,sqrt(2), 2*numN + 1);
yy = -sqrt(2)/numN./2.*mod(0:2*numN, 2);
plot(xx,yy, 'Color','k', 'LineWidth',4)
plot([0,sqrt(2)/2].*numA./numN - .15/numN, [0,sqrt(2)/2].*numA./numN + .15/numN, 'Color',[.8,0,0], 'LineWidth',6)
plot(sqrt(2) - [0,sqrt(2)/2].*numB./numN + .15/numN, [0,sqrt(2)/2].*numB./numN + .15/numN, 'Color',[0,0,.8], 'LineWidth',6)

% 在这里改分类名称
text(sqrt(2)/4.*numA./numN - .5/numN, sqrt(2)/4.*numA./numN + .5/numN, 'A',...
    'FontSize',36, 'FontName','Times New Roman','HorizontalAlignment','right')
text(sqrt(2) - sqrt(2)/4.*numB./numN + .5/numN, sqrt(2)/4.*numB./numN + .5/numN, 'B',...
    'FontSize',36, 'FontName','Times New Roman')

树状图在上方的倒三角热图

matlab 复制代码
% 随机生成数据
X=randn(20,30)+[(linspace(-1,2.5,20)').*ones(1,8),(linspace(-3,1,20)').*ones(1,5),(linspace(.5,-.7,20)').*ones(1,10),(linspace(.9,-.2,20)').*ones(1,7)];
Data = corr(X);
% 变量名列表
NameList = compose('Sl-%d',1:30);
% 分类配色 -- Color schemes for each clust
CList = [0.1490    0.4039    0.4980
         0.3882    0.3608    0.4471
         0.5373    0.2157    0.3098
         0.7686    0.4353    0.2431];


% 创建聚类树状图对象 -- create tree(dendrogram) object
% 左侧聚类树状图 -- left Cluster Tree
Z1 = linkage(Data, 'average');
ST = STree(Z1, 'MaxClust', 4); % 聚类数量,如果大于4请给CList多设置几个颜色
ST.Orientation = 'top';
ST.XLim = [0,sqrt(2)];
ST.YLim = [0,.25];
ST.Label = 'off';
ST.BranchColor = 'on';
ST.BranchHighlight = 'on';
ST.RTick = [0,1,1.2,0];
ST.CData = CList;
ST.draw();
%% ========================================================================
% 创建热图对象 -- create heatmap object
SM = SMatrix(Data);
% 添加分组信息 -- Add grouping information
SM.RowName = NameList;
SM.ColName = NameList;
SM.RowOrder = ST.order;
SM.RowClass = ST.class;
SM.ColOrder = ST.order;
SM.ColClass = ST.class;
% 设置文本和字体 -- Set Text and Font
SM.LeftLabel = 'off';
SM.RightLabel = 'on';
SM.BottomLabelFont = {'FontSize', 15, 'FontName', 'Times New Roman', 'Rotation', 45};
SM.RightLabelFont = {'FontSize', 15, 'FontName', 'Times New Roman'};
% 设置位置 -- set position
SM.XLim = [0,1];
SM.YLim = [0,1];
SM.TLim = [-pi/4,-pi/4];
SM.draw()
%% ========================================================================
% 修饰句柄 -- decorate handles
% 清除热图上半部分 -- Clear the bottom half of the heatmap
tData = tril(ones(size(Data)),-1);
tInd = find(tData(:) == 1);
for i = 1:length(tInd)
    set(SM.heatmapHdl{tInd(i)}, 'Visible', 'off')
end
tData = eye(size(Data));
tInd = find(tData(:) == 1);
for i = 1:length(tInd)
    SM.heatmapHdl{tInd(i)}.XData(SM.heatmapHdl{tInd(i)}.YData > 0) = [];
    SM.heatmapHdl{tInd(i)}.YData(SM.heatmapHdl{tInd(i)}.YData > 0) = [];
end
% 修饰坐标区域 -- decorate axes
set(gca, 'XColor', 'none', 'YColor', 'none',...
    'DataAspectRatio', [1,1,1],'XLim', [-.15,1.5], 'YLim', [-1.45/2, .26]);
CB = colorbar();
set(CB,'FontName','Times New Roman','FontSize',14);
CB.Position(4) = CB.Position(4)/3;

以上已经绘图部分代码,工具和绘图部分完整代码可在下方gitee仓库复刻二十二部分找到:

相关推荐
2zcode5 小时前
基于Matlab图像处理的水果分级系统
图像处理·人工智能·matlab
88号技师12 小时前
2025年7月Renewable Energy-冬虫夏草优化算法Caterpillar Fungus Optimizer-附Matlab免费代码
开发语言·人工智能·算法·matlab·优化算法
yugi9878381 天前
使用MATLAB生成三维圆柱形Voronoi图
开发语言·matlab
88号技师1 天前
2025年7月一区SCI-投影迭代优化算法Projection Iterative Methods-附Matlab免费代码
开发语言·人工智能·算法·机器学习·matlab·优化算法
WangYan20222 天前
MATLAB近红外光谱分析:MATLAB编程+BP神经网络+SVM+随机森林+遗传算法+变量降维+卷积神经网络等
matlab·bp神经网络·近红外光谱
SageFlower2 天前
MATLAB 数据掩膜函数
开发语言·matlab
2zcode2 天前
基于Matlab传统图像处理技术的车辆车型识别与分类方法研究
开发语言·图像处理·matlab
关岭风尘2 天前
Matlab/Simulink - BLDC直流无刷电机仿真基础教程(八) - 变电感法检测转子初始位置
matlab·bldc电机·变电感法·转子位置检测
仟濹3 天前
【数学建模|Matlab】Matlab「基础知识」和「基础操作」
数学建模·matlab