轉自:CSDN 本博客摘自天津科技大學謝中華老師一書, 在R語言中有根據矩陣繪制色塊圖的程序,可以用于繪制相關系數矩陣圖,用豐富的顏色和形狀形象的展示矩陣元素值的大小。今天介紹的是如何用matlab繪制矩陣色塊圖。 1、繪制網格線,網格中顯示矩陣元素,顯示黑色文字。 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'FillStyle','nofill','XVarNames',XVarNames,'YVarNames',XVarNames);
效果圖如下: 
2、根據矩陣元素值自動設置文字顏色。 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'FillStyle','nofill','XVarNames',XVarNames,'YVarNames',XVarNames,'TextColor','Auto','ColorBar','on');
效果圖如下: 
3、繪制實值矩陣各元素對應的色塊,通過不同的“參數/參數值”控制色塊的形狀、大小、顏色等屬性。 方形色塊,充滿方格,灰白色字體。 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'TextColor',[0.6,0.6,0.6],'ColorBar','on');
效果圖如下: 
4、方形色塊,根據矩陣元素值自動確定色塊的大小和顏色,不顯示字體。 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Auto','ColorBar','on');
效果圖如下: 
5、橢圓形色塊,根據矩陣元素值自動確定色塊的大小和顏色,不顯示字體。 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Auto','ColorBar','on','FigShape','e');
效果圖如下: 
6、圓形色塊,根據矩陣元素值自動確定色塊的大小和顏色,不顯示字體,上三角形式顯示。 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Auto','ColorBar','on','FigShape','c','FigStyle','Triu');
效果圖如下: 
7、六邊形色塊,根據矩陣元素值自動確定色塊的大小和顏色,顯示字體,下三角形式顯示 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','on','FigSize','Auto','ColorBar','on','FigShape','h','FigStyle','Tril');
效果圖如下: 
8、表盤形色塊,根據矩陣元素值自動確定色塊的顏色,不顯示字體 x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853 -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000]; XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'}; matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Full','ColorBar','on','FigShape','d');
效果圖如下: 
最后貼上matrixplot函數的源碼,源碼中有詳細的調用格式及參數說明,可以通過設置不同參數。感興趣的朋友可以自己設置不同的參數。 function matrixplot(data,varargin) % 根據實值矩陣繪制色塊圖,用豐富的顏色和形狀形象的展示矩陣元素值的大小。 % matrixplot(data) 繪制矩陣色塊圖,data為實值矩陣,每一個元素對應一個色塊,色 % matrixplot(data, 'PARAM1',val1, 'PARAM2',val2, ...) % 用成對出現的參數名/參數值控制色塊的各項屬性。可用的參數名/參數值如下: % 'FigShape' --- 設定色塊的形狀,其參數值為: % 'FigSize' --- 設定色塊的大小,其參數值為: % 'Auto' --- 根據矩陣元素值自動確定色塊大小 % 'FigStyle' --- 設定矩陣圖樣式,其參數值為: % 'FillStyle' --- 設定色塊填充樣式,其參數值為: % 'DisplayOpt' --- 設定是否在色塊中顯示矩陣元素值,其參數值為: % 'TextColor' --- 設定文字的顏色,其參數值為: % 表示單色的字符('r','g','b','y','m','c','w','k'),默認為黑色 % 1行3列的紅、綠、藍三元色灰度值向量([r,g,b]) % 'Auto' --- 根據矩陣元素值自動確定文字顏色 % 'XVarNames' --- 設定X軸方向需要顯示的變量名(默認為X1,X2,...),其參數值為: % 字符串矩陣或字符串元胞數組,若為字符串矩陣,其行數應與data的列數相同 % 若為字符串元胞數組,其長度應與data的列數相同。 % 'YVarNames' --- 設定Y軸方向需要顯示的變量名(默認為Y1,Y2,...),其參數值為: % 字符串矩陣或字符串元胞數組,若為字符串矩陣,其行數應與data的行數相同 % 若為字符串元胞數組,其長度應與data的行數相同。 % 'ColorBar' --- 設定是否顯示顏色條,其參數值為: % 'Grid' --- 設定是否顯示網格線,其參數值為: % x = [1,-0.2,0.3,0.8,-0.5 % matrixplot(x,'DisplayOpt','off'); % matrixplot(x,'FillStyle','nofill','TextColor','Auto'); % matrixplot(x,'TextColor',[0.7,0.7,0.7],'FigShap','s','FigSize','Auto','ColorBar','on'); % matrixplot(x,'TextColor','k','FigShap','d','FigSize','Full','ColorBar','on','FigStyle','Triu'); % XVarNames = {'xiezhh','heping','keda','tust','tianjin'}; % matrixplot(x,'FigShap','e','FigSize','Auto','ColorBar','on','XVarNames',XVarNames,'YVarNames',XVarNames); if ~ismatrix(data) || ~isreal(data) error('輸入參數類型不匹配:第一個輸入參數應為實值矩陣'); [FigShape,FigSize,FigStyle,FillStyle,DisplayOpt,TextColor,XVarNames,... YVarNames,ColorBar,GridOpt] = parseInputs(varargin{:}); [x,y] = meshgrid(0:n,0:m); rangedata = maxdata - mindata; warning('MATLAB:warning1','請檢查您輸入的矩陣是否合適!'); sx = x(1:end-1,1:end-1)+0.5; sy = y(1:end-1,1:end-1)+0.5; if strncmpi(FigStyle,'Tril',4) z(triu(ones(size(z)),2)>0) = NaN; sx(triu(ones(size(sx)),1)>0) = NaN; elseif strncmpi(FigStyle,'Triu',4) z(tril(ones(size(z)),-2)>0) = NaN; sx(tril(ones(size(sx)),-1)>0) = NaN; id = isnan(sx) | isnan(data); XVarNames = strcat('X',cellstr(num2str((1:n)'))); if (iscell(XVarNames) && (numel(XVarNames) ~= n)) || (~iscell(XVarNames) && (size(XVarNames,1) ~= n)) error('X軸方向變量名應為字符串矩陣或字符串元胞數組,其長度與輸入矩陣的列數相同'); YVarNames = strcat('Y',cellstr(num2str((1:m)'))); if (iscell(YVarNames) && (numel(YVarNames) ~= m)) || (~iscell(YVarNames) && (size(YVarNames,1) ~= m)) error('Y軸方向變量名應為字符串矩陣或字符串元胞數組,其長度與輸入矩陣的行數相同'); 'pos',[0.289165,0.154948,0.409956,0.68099]); axes('units','normalized','pos',[0.1,0.022,0.89,0.85]); if strncmpi(GridOpt,'On',2) 'EdgeColor',[0.7,0.7,0.7],... axis([-0.1,n+0.1,-0.1,m+0.1,-0.5,0.5]); set(gca,'Xtick',(1:n)-0.5,... 'XtickLabel',XVarNames,... 'YtickLabel',YVarNames,... 'XAxisLocation','top',... 'Xcolor',[0.7,0.7,0.7],... 'Ycolor',[0.7,0.7,0.7],... if strncmpi(FillStyle,'Fill',3) MyPatch(sx',sy',data',FigShape,FigSize); if strncmpi(DisplayOpt,'On',2) str = num2str(data,'%4.2f'); scale = 0.1*max(n/m,1)/(max(m,n)^0.55); if strncmpi(TextColor,'Auto',3) ColorMat = get(gcf,'ColorMap'); cid = fix(mapminmax(data',0,1)*nc)+1; TextColor = ColorMat(cid,:); text(sx(i),sy(i),0.1,str(i,:),... 'FontUnits','normalized',... 'HorizontalAlignment','center',... text(sx,sy,0.1*ones(size(sx)),str,... 'FontUnits','normalized',... 'HorizontalAlignment','center',... MyTickLabel(gca,FigStyle); if strncmpi(ColorBar,'On',2) if any(strncmpi(FigStyle,{'Auto','Triu'},4)) colorbar('Location','EastOutside'); colorbar('Location','SouthOutside'); % --------------------------------------------------- % --------------------------------------------------- function MyTickLabel(ha,tag) % tag 調整坐標軸刻度標簽的標識字符串,可用取值如下: % 'Auto' --- 將x軸刻度標簽旋轉90度,y軸刻度標簽不作調整 % 'Tril' --- 將x軸刻度標簽旋轉90度,并依次縮進,y軸刻度標簽不作調整 % 'Triu' --- 將x軸刻度標簽旋轉90度,y軸刻度標簽依次縮進 % MyTickLabel(gca,'Tril'); warning('MATLAB:warning2','第一個輸入參數應為坐標系句柄'); if ~strcmpi(get(ha,'type'),'axes') warning('MATLAB:warning3','第一個輸入參數應為坐標系句柄'); xstr = get(ha,'XTickLabel'); ystr = get(ha,'YTickLabel'); set(ha,'XTickLabel',[],'YTickLabel',[]); x = zeros(size(ytick)) + xl(1) - range(xl)/30; y = zeros(size(xtick)) + yl(1) - range(yl)/70; if strncmpi(tag,'Tril',4) elseif strncmpi(tag,'Triu',4) 'HorizontalAlignment','left'); 'HorizontalAlignment','right'); % --------------------------------------------------- % --------------------------------------------------- function MyPatch(x,y,z,FigShape,FigSize) % MyPatch(x,y,z,FigShape,FigSize) x,y,z是實值數組,用來指定色塊中心點三維 % 坐標。FigShape是字符串變量,用來指定色塊形狀。 % FigSize是字符串變量,用來指定色塊大小。 % MyPatch(x,y,z,'s','Auto'); if ~isreal(x) || ~isreal(y) || ~isreal(z) if numel(x) ~= n || numel(y) ~= n if strncmpi(FigSize,'Auto',3) && ~strncmpi(FigShape,'Ellipse',1) if strncmpi(FigShape,'Square',1) if strncmpi(FigSize,'Full',3) elseif strncmpi(FigShape,'Circle',1) if strncmpi(FigSize,'Full',3) elseif strncmpi(FigShape,'Ellipse',1) a = 0.48 + rab2*(0.57-0.48); EllipseVertices(x,y,z,a,b); elseif strncmpi(FigShape,'Hexagon',1) if strncmpi(FigSize,'Full',3) HexagonVertices(x,y,z,r); if strncmpi(FigSize,'Full',3) %-------------------------------------------------- %-------------------------------------------------- function SquareVertices(x,y,z,r) Xp = [x-hx;x-hx;x+hx;x+hx;x-hx]; Yp = [y-hy;y+hy;y+hy;y-hy;y-hy]; patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat'); function CircleVertices(x,y,z,r) t = linspace(0,2*pi,30)'; Xp = repmat(x,[m,1])+cos(t)*r; Yp = repmat(y,[m,1])+sin(t)*r; patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat'); function EllipseVertices(x,y,z,a,b) t = linspace(0,2*pi,30)'; Xp = repmat(x,[m,1]) + x0.*cos(t0) - y0.*sin(t0); Yp = repmat(y,[m,1]) + x0.*sin(t0) + y0.*cos(t0); patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat'); function HexagonVertices(x,y,z,r) Xp = repmat(x,[m,1])+cos(t)*r; Yp = repmat(y,[m,1])+sin(t)*r; patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat'); function DialVertices(x,y,z,r) t0 = cell2mat(arrayfun(@(x)linspace(-pi/2,x,30)',t0,'UniformOutput',0)); Xp = [x;repmat(x,[m,1]) + r0.*cos(t0);x]; Yp = [y;repmat(y,[m,1]) + r0.*sin(t0);y]; patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor',[0,0,0]); t = linspace(0,2*pi,30)'; Xp = repmat(x,[m,1])+cos(t)*r; Yp = repmat(y,[m,1])+sin(t)*r; patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor',[0,0,0]); %-------------------------------------------------------------------------- %-------------------------------------------------------------------------- function [FigShape,FigSize,FigStyle,FillStyle,DisplayOpt,TextColor,... XVarNames,YVarNames,ColorBar,GridOpt] = parseInputs(varargin) error('輸入參數個數不對,應為成對出現'); pnames = {'FigShape','FigSize','FigStyle','FillStyle','DisplayOpt',... 'TextColor','XVarNames','YVarNames','ColorBar','Grid'}; dflts = {'Square','Full','Auto','Fill','On','k','','','Off','On'}; [FigShape,FigSize,FigStyle,FillStyle,DisplayOpt,TextColor,XVarNames,... YVarNames,ColorBar,GridOpt] = parseArgs(pnames, dflts, varargin{:}); validateattributes(FigShape,{'char'},{'nonempty'},mfilename,'FigShape'); validateattributes(FigSize,{'char'},{'nonempty'},mfilename,'FigSize'); validateattributes(FigStyle,{'char'},{'nonempty'},mfilename,'FigStyle'); validateattributes(FillStyle,{'char'},{'nonempty'},mfilename,'FillStyle'); validateattributes(DisplayOpt,{'char'},{'nonempty'},mfilename,'DisplayOpt'); validateattributes(TextColor,{'char','numeric'},{'nonempty'},mfilename,'TextColor'); validateattributes(XVarNames,{'char','cell'},{},mfilename,'XVarNames'); validateattributes(YVarNames,{'char','cell'},{},mfilename,'YVarNames'); validateattributes(ColorBar,{'char'},{'nonempty'},mfilename,'ColorBar'); validateattributes(GridOpt,{'char'},{'nonempty'},mfilename,'Grid'); if ~any(strncmpi(FigShape,{'Square','Circle','Ellipse','Hexagon','Dial'},1)) error('形狀參數只能為Square, Circle, Ellipse, Hexagon, Dial 之一'); if ~any(strncmpi(FigSize,{'Full','Auto'},3)) error('圖形大小參數只能為Full, Auto 之一'); if ~any(strncmpi(FigStyle,{'Auto','Tril','Triu'},4)) error('圖形樣式參數只能為Auto, Tril, Triu 之一'); if ~any(strncmpi(FillStyle,{'Fill','NoFill'},3)) error('圖形填充樣式參數只能為Fill, NoFill 之一'); if ~any(strncmpi(DisplayOpt,{'On','Off'},2)) error('顯示數值參數只能為On,Off 之一'); if ~any(strncmpi(ColorBar,{'On','Off'},2)) error('顯示顏色條參數只能為On,Off 之一'); if ~any(strncmpi(GridOpt,{'On','Off'},2)) error('顯示網格參數只能為On,Off 之一'); %-------------------------------------------------------------------------- %-------------------------------------------------------------------------- function [varargout] = parseArgs(pnames,dflts,varargin) % Copyright 2010-2011 The MathWorks, Inc. % $Revision: 1.1.6.2 $ $Date: 2011/05/09 01:27:26 $ % Initialize some variables nparams = length(pnames); setflag = false(1,nparams); nargs = length(varargin); dosetflag = nargout>nparams; dounrecog = nargout>(nparams+1); % Must have name/value pairs m = message('stats:internal:parseArgs:WrongNumberArgs'); throwAsCaller(MException(m.Identifier, '%s', getString(m))); % Process name/value pairs m = message('stats:internal:parseArgs:IllegalParamName'); throwAsCaller(MException(m.Identifier, '%s', getString(m))); mask = strncmpi(pname,pnames,length(pname)); % look for partial match % if they've asked to get back unrecognized names/values, add this unrecog((end+1):(end+2)) = {varargin{j} varargin{j+1}}; % otherwise, it's an error m = message('stats:internal:parseArgs:BadParamName',pname); throwAsCaller(MException(m.Identifier, '%s', getString(m))); mask = strcmpi(pname,pnames); % use exact match to resolve ambiguity m = message('stats:internal:parseArgs:AmbiguousParamName',pname); throwAsCaller(MException(m.Identifier, '%s', getString(m))); varargout{mask} = varargin{j+1}; % Return extra stuff if requested varargout{nparams+1} = setflag; varargout{nparams+2} = unrecog;
|