赞
踩
我们在论文里经常能见到这样三角形的相关性热图,这样的热图绘制出来会更节省空间:
这期就教大家如何绘制三角形的热图,绘制效果如下:
以下是教程部分:
这里随便生成了一组数据,计算了相关系数,同时给出了各个变量名:
% 随机生成数据
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(X1);
% 变量名列表
NameList=compose('Sl-%d',1:20);
% 图窗及坐标区域 treeFig=figure('Position',[100,300,1000,600]); ax=axes(treeFig); ax.NextPlot='add'; ax.DataAspectRatio=[1,1,1]; ax.XColor='none'; ax.YColor='none'; % 热图绘制 sqX=[-1,0,1,0]; sqY=[0,1,0,-1]; for i=1:size(Data,1) for j=i:size(Data,1) fill(ax,sqX+(i-1)+(j-1),sqY-(i-1)+(j-1),Data(i,j),'EdgeColor','none') end end axis(ax,'tight')
比如说winter
配色:
% 设置颜色并绘制colorbar
clim(ax,[-1,1])
colormap(ax,'winter')
cbHdl=colorbar(ax);
cbHdl.Location='southoutside';
cbHdl.FontName='Times New Roman';
cbHdl.FontSize=12;
想要更丰富配色可以使用slanCM工具:
https://slandarer.blog.csdn.net/article/details/127719784
例如:
colormap(ax,slanCM(98))
想要下面文本就下面文本附近if后改为true
,想要右上方文本同理:
% 下方注释
if true
for i=1:size(Data,1)
text(ax,-1/2+2*(i-1),-1/2,NameList{i}+" ",'FontSize',13,...
'FontName','Times New Roman','HorizontalAlignment','right','Rotation',45)
end
end
% 右上方注释
if true
for i=1:size(Data,1)
text(ax,-1/2+(i-1)+size(Data,1),-1/2+size(Data,1)-(i-1)," "+NameList{i},'FontSize',13,...
'FontName','Times New Roman','HorizontalAlignment','left','Rotation',45)
end
end
此部分需要下载Statistics and Machine Learning Toolbox工具箱:
由于树状图需要把矩阵重新排列,因此单独写段代码:
% Rotation 45-degree corr heatmap % 随机生成数据 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); % 图窗及坐标区域 treeFig=figure('Position',[100,300,1000,600]); ax=axes(treeFig); ax.NextPlot='add'; ax.DataAspectRatio=[1,1,1]; ax.XColor='none'; ax.YColor='none'; % 获取树状图数据 fig1=figure(); tree1=linkage(Data,'average'); [~,~,order1]=dendrogram(tree1,0,'Orientation','top'); Data=Data(order1,order1); NameList=NameList(order1); LineSet1=fig1.Children.Children; maxY1=0; for i=1:length(LineSet1) maxY1=max(max(LineSet1(i).YData),maxY1); end for i=1:length(LineSet1) LineSet1(i).YData=LineSet1(i).YData./maxY1.*size(Data,1)./3; end for i=1:length(LineSet1) tX=(LineSet1(i).XData-1).*sqrt(2); tY=LineSet1(i).YData; nX=tX.*cos(pi/4)-tY.*sin(pi/4); nY=tX.*sin(pi/4)+tY.*cos(pi/4); plot(ax,nX,nY,'Color','k','LineWidth',1); end delete(fig1) % 热图绘制 sqX=[-1,0,1,0]; sqY=[0,1,0,-1]; for i=1:size(Data,1) for j=i:size(Data,1) fill(ax,sqX+(i-1)+(j-1),sqY-(i-1)+(j-1),Data(i,j),'EdgeColor','none') end end axis(ax,'tight') % 设置颜色并绘制colorbar clim(ax,[-1,1]) colormap(ax,'winter') cbHdl=colorbar(ax); cbHdl.Location='southoutside'; cbHdl.FontName='Times New Roman'; cbHdl.FontSize=12; colormap(ax,slanCM(100)) for i=1:size(Data,1) text(ax,-1/2+(i-1)+size(Data,1),-1/2+size(Data,1)-(i-1)," "+NameList{i},'FontSize',13,... 'FontName','Times New Roman','HorizontalAlignment','left','Rotation',45) end
展示一下使用slanCM后的其他配色:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。