醉飲千殤v / 待分類 / matlab繪制矩陣色塊圖

分享

   

matlab繪制矩陣色塊圖

2020-07-13  醉飲千殤v

轉自:CSDN

本博客摘自天津科技大學謝中華老師一書,

           在R語言中有根據矩陣繪制色塊圖的程序,可以用于繪制相關系數矩陣圖,用豐富的顏色和形狀形象的展示矩陣元素值的大小。今天介紹的是如何用matlab繪制矩陣色塊圖。

1、繪制網格線,網格中顯示矩陣元素,顯示黑色文字。

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'FillStyle','nofill','XVarNames',XVarNames,'YVarNames',XVarNames);

效果圖如下:

2、根據矩陣元素值自動設置文字顏色。

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'FillStyle','nofill','XVarNames',XVarNames,'YVarNames',XVarNames,'TextColor','Auto','ColorBar','on');

效果圖如下:

3、繪制實值矩陣各元素對應的色塊,通過不同的“參數/參數值”控制色塊的形狀、大小、顏色等屬性。 
方形色塊,充滿方格,灰白色字體。

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'TextColor',[0.6,0.6,0.6],'ColorBar','on');

效果圖如下:

4、方形色塊,根據矩陣元素值自動確定色塊的大小和顏色,不顯示字體。

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Auto','ColorBar','on');

效果圖如下:

5、橢圓形色塊,根據矩陣元素值自動確定色塊的大小和顏色,不顯示字體。

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Auto','ColorBar','on','FigShape','e');

效果圖如下:

6、圓形色塊,根據矩陣元素值自動確定色塊的大小和顏色,不顯示字體,上三角形式顯示。

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Auto','ColorBar','on','FigShape','c','FigStyle','Triu');

效果圖如下:

7、六邊形色塊,根據矩陣元素值自動確定色塊的大小和顏色,顯示字體,下三角形式顯示

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','on','FigSize','Auto','ColorBar','on','FigShape','h','FigStyle','Tril');

效果圖如下:

8、表盤形色塊,根據矩陣元素值自動確定色塊的顏色,不顯示字體

  1. x = [1.0000 0.1302 0.0582 0.0169 0.0779 0.0833 -0.0313
  2. 0.1302 1.0000 0.0482 0.3843 0.2105 -0.2500 0.0803
  3. 0.0582 0.0482 1.0000 0.0481 0.3515 -0.0426 0.0574
  4. 0.0169 0.3843 0.0481 1.0000 -0.1819 -0.3554 -0.0020
  5. 0.0779 0.2105 0.3515 -0.1819 1.0000 0.0388 0.0186
  6. 0.0833 -0.2500 -0.0426 -0.3554 0.0388 1.0000 -0.1853
  7. -0.0313 0.0803 0.0574 -0.0020 0.0186 -0.1853 1.0000];
  8. XVarNames = {'Sleep','Age','Sex','Rel','Psy','Ner','Cha'};
  9. matrixplot(x,'XVarNames',XVarNames,'YVarNames',XVarNames,'DisplayOpt','off','FigSize','Full','ColorBar','on','FigShape','d');

效果圖如下:

 最后貼上matrixplot函數的源碼,源碼中有詳細的調用格式及參數說明,可以通過設置不同參數。感興趣的朋友可以自己設置不同的參數。

  1. function matrixplot(data,varargin)
  2. % 根據實值矩陣繪制色塊圖,用豐富的顏色和形狀形象的展示矩陣元素值的大小。
  3. %
  4. % matrixplot(data) 繪制矩陣色塊圖,data為實值矩陣,每一個元素對應一個色塊,色
  5. % 塊顏色由元素值大小決定。
  6. %
  7. % matrixplot(data, 'PARAM1',val1, 'PARAM2',val2, ...)
  8. % 用成對出現的參數名/參數值控制色塊的各項屬性。可用的參數名/參數值如下:
  9. % 'FigShape' --- 設定色塊的形狀,其參數值為:
  10. % 'Square' --- 方形(默認)
  11. % 'Circle' --- 圓形
  12. % 'Ellipse' --- 橢圓形
  13. % 'Hexagon' --- 六邊形
  14. % 'Dial' --- 表盤形
  15. %
  16. % 'FigSize' --- 設定色塊的大小,其參數值為:
  17. % 'Full' --- 最大色塊(默認)
  18. % 'Auto' --- 根據矩陣元素值自動確定色塊大小
  19. %
  20. % 'FigStyle' --- 設定矩陣圖樣式,其參數值為:
  21. % 'Auto' --- 矩形矩陣圖(默認)
  22. % 'Tril' --- 下三角矩陣圖
  23. % 'Triu' --- 上三角矩陣圖
  24. %
  25. % 'FillStyle' --- 設定色塊填充樣式,其參數值為:
  26. % 'Fill' --- 填充色塊內部(默認)
  27. % 'NoFill' --- 不填充色塊內部
  28. %
  29. % 'DisplayOpt' --- 設定是否在色塊中顯示矩陣元素值,其參數值為:
  30. % 'On' --- 顯示矩陣元素值(默認)
  31. % 'Off' --- 不顯示矩陣元素值
  32. %
  33. % 'TextColor' --- 設定文字的顏色,其參數值為:
  34. % 表示單色的字符('r','g','b','y','m','c','w','k'),默認為黑色
  35. % 1行3列的紅、綠、藍三元色灰度值向量([r,g,b])
  36. % 'Auto' --- 根據矩陣元素值自動確定文字顏色
  37. %
  38. % 'XVarNames' --- 設定X軸方向需要顯示的變量名(默認為X1,X2,...),其參數值為:
  39. % 字符串矩陣或字符串元胞數組,若為字符串矩陣,其行數應與data的列數相同
  40. % 若為字符串元胞數組,其長度應與data的列數相同。
  41. %
  42. % 'YVarNames' --- 設定Y軸方向需要顯示的變量名(默認為Y1,Y2,...),其參數值為:
  43. % 字符串矩陣或字符串元胞數組,若為字符串矩陣,其行數應與data的行數相同
  44. % 若為字符串元胞數組,其長度應與data的行數相同。
  45. %
  46. % 'ColorBar' --- 設定是否顯示顏色條,其參數值為:
  47. % 'On' --- 顯示顏色條
  48. % 'Off' --- 不顯示顏色條(默認)
  49. %
  50. % 'Grid' --- 設定是否顯示網格線,其參數值為:
  51. % 'On' --- 顯示網格線(默認)
  52. % 'Off' --- 不顯示網格線
  53. %
  54. % Example:
  55. % x = [1,-0.2,0.3,0.8,-0.5
  56. % -0.2,1,0.6,-0.7,0.2
  57. % 0.3,0.6,1,0.5,-0.3
  58. % 0.8,-0.7,0.5,1,0.7
  59. % -0.5,0.2,-0.3,0.7,1];
  60. % matrixplot(x);
  61. % matrixplot(x,'DisplayOpt','off');
  62. % matrixplot(x,'FillStyle','nofill','TextColor','Auto');
  63. % matrixplot(x,'TextColor',[0.7,0.7,0.7],'FigShap','s','FigSize','Auto','ColorBar','on');
  64. % matrixplot(x,'TextColor','k','FigShap','d','FigSize','Full','ColorBar','on','FigStyle','Triu');
  65. % XVarNames = {'xiezhh','heping','keda','tust','tianjin'};
  66. % matrixplot(x,'FigShap','e','FigSize','Auto','ColorBar','on','XVarNames',XVarNames,'YVarNames',XVarNames);
  67. % 對第一個輸入參數類型進行判斷
  68. if ~ismatrix(data) || ~isreal(data)
  69. error('輸入參數類型不匹配:第一個輸入參數應為實值矩陣');
  70. end
  71. % 解析成對出現的參數名/參數值
  72. [FigShape,FigSize,FigStyle,FillStyle,DisplayOpt,TextColor,XVarNames,...
  73. YVarNames,ColorBar,GridOpt] = parseInputs(varargin{:});
  74. % 產生網格數據
  75. [m,n] = size(data);
  76. [x,y] = meshgrid(0:n,0:m);
  77. data = data(:);
  78. maxdata = nanmax(data);
  79. mindata = nanmin(data);
  80. rangedata = maxdata - mindata;
  81. if isnan(rangedata)
  82. warning('MATLAB:warning1','請檢查您輸入的矩陣是否合適!');
  83. return;
  84. end
  85. z = zeros(size(x))+0.2;
  86. sx = x(1:end-1,1:end-1)+0.5;
  87. sy = y(1:end-1,1:end-1)+0.5;
  88. if strncmpi(FigStyle,'Tril',4)
  89. z(triu(ones(size(z)),2)>0) = NaN;
  90. sx(triu(ones(size(sx)),1)>0) = NaN;
  91. elseif strncmpi(FigStyle,'Triu',4)
  92. z(tril(ones(size(z)),-2)>0) = NaN;
  93. sx(tril(ones(size(sx)),-1)>0) = NaN;
  94. end
  95. sx = sx(:);
  96. sy = sy(:);
  97. id = isnan(sx) | isnan(data);
  98. sx(id) = [];
  99. sy(id) = [];
  100. data(id) = [];
  101. if isempty(XVarNames)
  102. XVarNames = strcat('X',cellstr(num2str((1:n)')));
  103. else
  104. if (iscell(XVarNames) && (numel(XVarNames) ~= n)) || (~iscell(XVarNames) && (size(XVarNames,1) ~= n))
  105. error('X軸方向變量名應為字符串矩陣或字符串元胞數組,其長度與輸入矩陣的列數相同');
  106. end
  107. end
  108. if isempty(YVarNames)
  109. YVarNames = strcat('Y',cellstr(num2str((1:m)')));
  110. else
  111. if (iscell(YVarNames) && (numel(YVarNames) ~= m)) || (~iscell(YVarNames) && (size(YVarNames,1) ~= m))
  112. error('Y軸方向變量名應為字符串矩陣或字符串元胞數組,其長度與輸入矩陣的行數相同');
  113. end
  114. end
  115. % 繪圖
  116. figure('color','w',...
  117. 'units','normalized',...
  118. 'pos',[0.289165,0.154948,0.409956,0.68099]);
  119. axes('units','normalized','pos',[0.1,0.022,0.89,0.85]);
  120. if strncmpi(GridOpt,'On',2)
  121. mesh(x,y,z,...
  122. 'EdgeColor',[0.7,0.7,0.7],...
  123. 'FaceAlpha',0,...
  124. 'LineWidth',1); % 參考網格線
  125. end
  126. hold on;
  127. axis equal;
  128. axis([-0.1,n+0.1,-0.1,m+0.1,-0.5,0.5]);
  129. view(2);
  130. % 設置X軸和Y軸刻度位置及標簽
  131. set(gca,'Xtick',(1:n)-0.5,...
  132. 'XtickLabel',XVarNames,...
  133. 'Ytick',(1:m)-0.5,...
  134. 'YtickLabel',YVarNames,...
  135. 'XAxisLocation','top',...
  136. 'YDir','reverse',...
  137. 'Xcolor',[0.7,0.7,0.7],...
  138. 'Ycolor',[0.7,0.7,0.7],...
  139. 'TickLength',[0,0]);
  140. axis off
  141. % 繪制填充色塊
  142. if strncmpi(FillStyle,'Fill',3)
  143. MyPatch(sx',sy',data',FigShape,FigSize);
  144. end
  145. % 顯示數值文本信息
  146. if strncmpi(DisplayOpt,'On',2)
  147. str = num2str(data,'%4.2f');
  148. scale = 0.1*max(n/m,1)/(max(m,n)^0.55);
  149. if strncmpi(TextColor,'Auto',3)
  150. ColorMat = get(gcf,'ColorMap');
  151. nc = size(ColorMat,1);
  152. cid = fix(mapminmax(data',0,1)*nc)+1;
  153. cid(cid<1) = 1;
  154. cid(cid>nc) = nc;
  155. TextColor = ColorMat(cid,:);
  156. for i = 1:numel(data)
  157. text(sx(i),sy(i),0.1,str(i,:),...
  158. 'FontUnits','normalized',...
  159. 'FontSize',scale,...
  160. 'fontweight','bold',...
  161. 'HorizontalAlignment','center',...
  162. 'Color',TextColor(i,:));
  163. end
  164. else
  165. text(sx,sy,0.1*ones(size(sx)),str,...
  166. 'FontUnits','normalized',...
  167. 'FontSize',scale,...
  168. 'fontweight','bold',...
  169. 'HorizontalAlignment','center',...
  170. 'Color',TextColor);
  171. end
  172. end
  173. % 設置X軸和Y軸刻度標簽的縮進方式
  174. MyTickLabel(gca,FigStyle);
  175. % 添加顏色條
  176. if strncmpi(ColorBar,'On',2)
  177. if any(strncmpi(FigStyle,{'Auto','Triu'},4))
  178. colorbar('Location','EastOutside');
  179. else
  180. colorbar('Location','SouthOutside');
  181. end
  182. end
  183. end
  184. % ---------------------------------------------------
  185. % 調整坐標軸刻度標簽子函數
  186. % ---------------------------------------------------
  187. function MyTickLabel(ha,tag)
  188. % 根據顯示范圍自動調整坐標軸刻度標簽的函數
  189. % ha 坐標系句柄值
  190. % tag 調整坐標軸刻度標簽的標識字符串,可用取值如下:
  191. % 'Auto' --- 將x軸刻度標簽旋轉90度,y軸刻度標簽不作調整
  192. % 'Tril' --- 將x軸刻度標簽旋轉90度,并依次縮進,y軸刻度標簽不作調整
  193. % 'Triu' --- 將x軸刻度標簽旋轉90度,y軸刻度標簽依次縮進
  194. % Example:
  195. % MyTickLabel(gca,'Tril');
  196. if ~ishandle(ha)
  197. warning('MATLAB:warning2','第一個輸入參數應為坐標系句柄');
  198. return;
  199. end
  200. if ~strcmpi(get(ha,'type'),'axes')
  201. warning('MATLAB:warning3','第一個輸入參數應為坐標系句柄');
  202. return;
  203. end
  204. axes(ha);
  205. xstr = get(ha,'XTickLabel');
  206. xtick = get(ha,'XTick');
  207. xl = xlim(ha);
  208. ystr = get(ha,'YTickLabel');
  209. ytick = get(ha,'YTick');
  210. yl = ylim(ha);
  211. set(ha,'XTickLabel',[],'YTickLabel',[]);
  212. x = zeros(size(ytick)) + xl(1) - range(xl)/30;
  213. y = zeros(size(xtick)) + yl(1) - range(yl)/70;
  214. nx = numel(xtick);
  215. ny = numel(ytick);
  216. if strncmpi(tag,'Tril',4)
  217. y = y + (1:nx) - 1;
  218. elseif strncmpi(tag,'Triu',4)
  219. x = x + (1:ny) - 1;
  220. end
  221. text(xtick,y,xstr,...
  222. 'rotation',90,...
  223. 'Interpreter','none',...
  224. 'color','r',...
  225. 'HorizontalAlignment','left');
  226. text(x,ytick,ystr,...
  227. 'Interpreter','none',...
  228. 'color','r',...
  229. 'HorizontalAlignment','right');
  230. end
  231. % ---------------------------------------------------
  232. % 根據散點數據繪制3維色塊圖子函數
  233. % ---------------------------------------------------
  234. function MyPatch(x,y,z,FigShape,FigSize)
  235. % 根據散點數據繪制3維色塊圖
  236. % MyPatch(x,y,z,FigShape,FigSize) x,y,z是實值數組,用來指定色塊中心點三維
  237. % 坐標。FigShape是字符串變量,用來指定色塊形狀。
  238. % FigSize是字符串變量,用來指定色塊大小。
  239. %
  240. % Example:
  241. % x = rand(10,1);
  242. % y = rand(10,1);
  243. % z = rand(10,1);
  244. % MyPatch(x,y,z,'s','Auto');
  245. %
  246. % 輸入參數類型判斷
  247. if nargin < 3
  248. error('至少需要三個輸入參數');
  249. end
  250. if ~isreal(x) || ~isreal(y) || ~isreal(z)
  251. error('前三個輸入應為實值數組');
  252. end
  253. n = numel(z);
  254. if numel(x) ~= n || numel(y) ~= n
  255. error('坐標應等長');
  256. end
  257. if strncmpi(FigSize,'Auto',3) && ~strncmpi(FigShape,'Ellipse',1)
  258. id = (z == 0);
  259. x(id) = [];
  260. y(id) = [];
  261. z(id) = [];
  262. end
  263. if isempty(z)
  264. return;
  265. end
  266. % 求色塊頂點坐標
  267. rab1 = ones(size(z));
  268. maxz = max(abs(z));
  269. if maxz == 0
  270. maxz = 1;
  271. end
  272. rab2 = abs(z)/maxz;
  273. if strncmpi(FigShape,'Square',1)
  274. % 方形
  275. if strncmpi(FigSize,'Full',3)
  276. r = rab1;
  277. else
  278. r = sqrt(rab2);
  279. end
  280. SquareVertices(x,y,z,r);
  281. elseif strncmpi(FigShape,'Circle',1)
  282. % 圓形
  283. if strncmpi(FigSize,'Full',3)
  284. r = 0.5*rab1;
  285. else
  286. r = 0.5*sqrt(rab2);
  287. end
  288. CircleVertices(x,y,z,r);
  289. elseif strncmpi(FigShape,'Ellipse',1)
  290. % 橢圓形
  291. a = 0.48 + rab2*(0.57-0.48);
  292. b = (1-rab2).*a;
  293. EllipseVertices(x,y,z,a,b);
  294. elseif strncmpi(FigShape,'Hexagon',1)
  295. % 六邊形
  296. if strncmpi(FigSize,'Full',3)
  297. r = 0.5*rab1;
  298. else
  299. r = 0.5*sqrt(rab2);
  300. end
  301. HexagonVertices(x,y,z,r);
  302. else
  303. % 表盤形
  304. if strncmpi(FigSize,'Full',3)
  305. r = 0.45*rab1;
  306. else
  307. r = 0.45*sqrt(rab2);
  308. end
  309. DialVertices(x,y,z,r);
  310. end
  311. end
  312. %--------------------------------------------------
  313. % 求色塊頂點坐標并繪制色塊的子函數
  314. %--------------------------------------------------
  315. function SquareVertices(x,y,z,r)
  316. % 方形
  317. hx = r/2;
  318. hy = hx;
  319. Xp = [x-hx;x-hx;x+hx;x+hx;x-hx];
  320. Yp = [y-hy;y+hy;y+hy;y-hy;y-hy];
  321. Zp = repmat(z,[5,1]);
  322. patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat');
  323. end
  324. function CircleVertices(x,y,z,r)
  325. % 圓形
  326. t = linspace(0,2*pi,30)';
  327. m = numel(t);
  328. Xp = repmat(x,[m,1])+cos(t)*r;
  329. Yp = repmat(y,[m,1])+sin(t)*r;
  330. Zp = repmat(z,[m,1]);
  331. patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat');
  332. end
  333. function EllipseVertices(x,y,z,a,b)
  334. % 橢圓形
  335. t = linspace(0,2*pi,30)';
  336. m = numel(t);
  337. t0 = -sign(z)*pi/4;
  338. t0 = repmat(t0,[m,1]);
  339. x0 = cos(t)*a;
  340. y0 = sin(t)*b;
  341. Xp = repmat(x,[m,1]) + x0.*cos(t0) - y0.*sin(t0);
  342. Yp = repmat(y,[m,1]) + x0.*sin(t0) + y0.*cos(t0);
  343. Zp = repmat(z,[m,1]);
  344. patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat');
  345. end
  346. function HexagonVertices(x,y,z,r)
  347. % 六邊形
  348. t = linspace(0,2*pi,7)';
  349. m = numel(t);
  350. Xp = repmat(x,[m,1])+cos(t)*r;
  351. Yp = repmat(y,[m,1])+sin(t)*r;
  352. Zp = repmat(z,[m,1]);
  353. patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor','flat');
  354. end
  355. function DialVertices(x,y,z,r)
  356. % 表盤形
  357. % 繪制表盤扇形
  358. maxz = max(abs(z));
  359. t0 = z*2*pi/maxz-pi/2;
  360. t0 = cell2mat(arrayfun(@(x)linspace(-pi/2,x,30)',t0,'UniformOutput',0));
  361. m = size(t0,1);
  362. r0 = repmat(r,[m,1]);
  363. Xp = [x;repmat(x,[m,1]) + r0.*cos(t0);x];
  364. Yp = [y;repmat(y,[m,1]) + r0.*sin(t0);y];
  365. Zp = repmat(z,[m+2,1]);
  366. patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor',[0,0,0]);
  367. % 繪制表盤圓周
  368. t = linspace(0,2*pi,30)';
  369. m = numel(t);
  370. Xp = repmat(x,[m,1])+cos(t)*r;
  371. Yp = repmat(y,[m,1])+sin(t)*r;
  372. Zp = repmat(z,[m,1]);
  373. Xp = [Xp;flipud(Xp)];
  374. Yp = [Yp;flipud(Yp)];
  375. Zp = [Zp;flipud(Zp)];
  376. patch(Xp,Yp,Zp,'FaceColor','flat','EdgeColor',[0,0,0]);
  377. end
  378. %--------------------------------------------------------------------------
  379. % 解析輸入參數子函數1
  380. %--------------------------------------------------------------------------
  381. function [FigShape,FigSize,FigStyle,FillStyle,DisplayOpt,TextColor,...
  382. XVarNames,YVarNames,ColorBar,GridOpt] = parseInputs(varargin)
  383. if mod(nargin,2)~=0
  384. error('輸入參數個數不對,應為成對出現');
  385. end
  386. pnames = {'FigShape','FigSize','FigStyle','FillStyle','DisplayOpt',...
  387. 'TextColor','XVarNames','YVarNames','ColorBar','Grid'};
  388. dflts = {'Square','Full','Auto','Fill','On','k','','','Off','On'};
  389. [FigShape,FigSize,FigStyle,FillStyle,DisplayOpt,TextColor,XVarNames,...
  390. YVarNames,ColorBar,GridOpt] = parseArgs(pnames, dflts, varargin{:});
  391. validateattributes(FigShape,{'char'},{'nonempty'},mfilename,'FigShape');
  392. validateattributes(FigSize,{'char'},{'nonempty'},mfilename,'FigSize');
  393. validateattributes(FigStyle,{'char'},{'nonempty'},mfilename,'FigStyle');
  394. validateattributes(FillStyle,{'char'},{'nonempty'},mfilename,'FillStyle');
  395. validateattributes(DisplayOpt,{'char'},{'nonempty'},mfilename,'DisplayOpt');
  396. validateattributes(TextColor,{'char','numeric'},{'nonempty'},mfilename,'TextColor');
  397. validateattributes(XVarNames,{'char','cell'},{},mfilename,'XVarNames');
  398. validateattributes(YVarNames,{'char','cell'},{},mfilename,'YVarNames');
  399. validateattributes(ColorBar,{'char'},{'nonempty'},mfilename,'ColorBar');
  400. validateattributes(GridOpt,{'char'},{'nonempty'},mfilename,'Grid');
  401. if ~any(strncmpi(FigShape,{'Square','Circle','Ellipse','Hexagon','Dial'},1))
  402. error('形狀參數只能為Square, Circle, Ellipse, Hexagon, Dial 之一');
  403. end
  404. if ~any(strncmpi(FigSize,{'Full','Auto'},3))
  405. error('圖形大小參數只能為Full, Auto 之一');
  406. end
  407. if ~any(strncmpi(FigStyle,{'Auto','Tril','Triu'},4))
  408. error('圖形樣式參數只能為Auto, Tril, Triu 之一');
  409. end
  410. if ~any(strncmpi(FillStyle,{'Fill','NoFill'},3))
  411. error('圖形填充樣式參數只能為Fill, NoFill 之一');
  412. end
  413. if ~any(strncmpi(DisplayOpt,{'On','Off'},2))
  414. error('顯示數值參數只能為On,Off 之一');
  415. end
  416. if ~any(strncmpi(ColorBar,{'On','Off'},2))
  417. error('顯示顏色條參數只能為On,Off 之一');
  418. end
  419. if ~any(strncmpi(GridOpt,{'On','Off'},2))
  420. error('顯示網格參數只能為On,Off 之一');
  421. end
  422. end
  423. %--------------------------------------------------------------------------
  424. % 解析輸入參數子函數2
  425. %--------------------------------------------------------------------------
  426. function [varargout] = parseArgs(pnames,dflts,varargin)
  427. % Copyright 2010-2011 The MathWorks, Inc.
  428. % $Revision: 1.1.6.2 $ $Date: 2011/05/09 01:27:26 $
  429. % Initialize some variables
  430. nparams = length(pnames);
  431. varargout = dflts;
  432. setflag = false(1,nparams);
  433. unrecog = {};
  434. nargs = length(varargin);
  435. dosetflag = nargout>nparams;
  436. dounrecog = nargout>(nparams+1);
  437. % Must have name/value pairs
  438. if mod(nargs,2)~=0
  439. m = message('stats:internal:parseArgs:WrongNumberArgs');
  440. throwAsCaller(MException(m.Identifier, '%s', getString(m)));
  441. end
  442. % Process name/value pairs
  443. for j=1:2:nargs
  444. pname = varargin{j};
  445. if ~ischar(pname)
  446. m = message('stats:internal:parseArgs:IllegalParamName');
  447. throwAsCaller(MException(m.Identifier, '%s', getString(m)));
  448. end
  449. mask = strncmpi(pname,pnames,length(pname)); % look for partial match
  450. if ~any(mask)
  451. if dounrecog
  452. % if they've asked to get back unrecognized names/values, add this
  453. % one to the list
  454. unrecog((end+1):(end+2)) = {varargin{j} varargin{j+1}};
  455. continue
  456. else
  457. % otherwise, it's an error
  458. m = message('stats:internal:parseArgs:BadParamName',pname);
  459. throwAsCaller(MException(m.Identifier, '%s', getString(m)));
  460. end
  461. elseif sum(mask)>1
  462. mask = strcmpi(pname,pnames); % use exact match to resolve ambiguity
  463. if sum(mask)~=1
  464. m = message('stats:internal:parseArgs:AmbiguousParamName',pname);
  465. throwAsCaller(MException(m.Identifier, '%s', getString(m)));
  466. end
  467. end
  468. varargout{mask} = varargin{j+1};
  469. setflag(mask) = true;
  470. end
  471. % Return extra stuff if requested
  472. if dosetflag
  473. varargout{nparams+1} = setflag;
  474. if dounrecog
  475. varargout{nparams+2} = unrecog;
  476. end
  477. end
  478. end

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。

    0條評論

    發表

    請遵守用戶 評論公約

    類似文章 更多
    喜歡該文的人也喜歡 更多

    ×
    ×

    .00

    微信或支付寶掃碼支付:

    開通即同意《個圖VIP服務協議》

    全部>>
    一级a做爰片,夜夜骑网站,黄色视频做爰视频,很色的床上视频