[已解决] Gridlegend函数用法

[复制链接]
shmilya 发表于 2018-6-9 16:38:37
求助各位大神,我在用Gridlegend函数时总会报错,实在不知道是什么问题,代码已附上。
  1. clear ;
  2. clc;
  3. figure(1);
  4. cxd = rand(10,5)*0.4;  %%%%% 产生5个0到0.4的随机数
  5. h2=plot(cxd);
  6. hleg = legend('a','b','c','d','e');
  7. set(hleg,'FontSize',9,'FontName','Times New Roman','Orientation','Horizontal');
  8. gridLegend(hdl,2)
复制代码


最佳答案


谱宇 发表于 2018-6-10 12:45:36
尝试一下这个程序:

步骤一:将以下程序保存为Multiplot.m
  1. clc
  2. clear all
  3. close all

  4. nLines = 18;
  5. legend_str = cell(nLines,1);

  6. % myColors = distinguishable_colors(nLines);
  7. myColors = jet(nLines);
  8. t = 0:pi/64:3*pi;
  9. dPhi = pi/16;
  10. lineStyles = {'-', '--', ':', '-.'};
  11. for ii=1:nLines,
  12.     plot(t,sin(t+dPhi*ii),...
  13.         'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...
  14.         'color', myColors(ii,:),...
  15.         'linewidth',3);
  16.     hold on;
  17.     legend_str{ii} = num2str(ii);
  18. end
  19. axis([0 3*pi -1.15 1.6])
  20. legend(legend_str,'location','NorthWest')

  21. for ii=1:nLines,
  22.     plot(t,sin(t+dPhi*ii),...
  23.         'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...
  24.         'color', myColors(ii,:),...
  25.         'linewidth',3);
  26.     hold on;
  27.     legend_str{ii} = num2str(ii);
  28. end
  29. axis([0 3*pi -1.15 1.6])
  30. columnlegend(6,legend_str,'NorthWest');
复制代码


步骤二:将下面这个程序保存为columnlegend.m
  1. function [legend_h,object_h,plot_h,text_strings] = columnlegend(numcolumns, str, varargin)
  2. %
  3. %   columnlegend creates a legend with a specified number of columns.
  4. %   
  5. %   columnlegend(numcolumns, str, varargin)
  6. %       numcolumns - number of columns in the legend
  7. %       str - cell array of strings for the legend
  8. %
  9. %   Additional Input Options:
  10. %   columnlegend(..., 'Location', loc)
  11. %       loc - location variable for legend, default is 'NorthEast'
  12. %                  possible values: 'NorthWest', 'NorthEast', 'SouthEast', 'SouthWest',
  13. %                                   'NorthOutside', 'SouthOutside',
  14. %                                   'NortheastOutside', 'SoutheastOutside'
  15. %
  16. %   columnlegend(numcolumns, str, 'padding', 0.5); % add 50% vertical padding between legend entries (relative to original height)
  17. %      
  18. %   columnlegend(..., 'boxon')
  19. %   columnlegend(..., 'boxoff')
  20. %        set legend bounding box on/off
  21. %
  22. %   example:
  23. %      plot(bsxfun(@times, [0:9]',[1:10]));
  24. %      columnlegend(3, cellstr(num2str([1:10]')), 'location','northwest');
  25. %
  26. %
  27. %   Author: Simon Henin <shenin@gc.cuny.edu>
  28. %   
  29. %   4/09/2013 - Fixed bug with 3 entries / 3 columns
  30. %   4/09/2013 - Added bounding box option as per @Durga Lal Shrestha (fileexchage)
  31. %   11 May 2010 - 1.2 Add instructions for printing figure with columns
  32. %   08 Feb 2011 - 1.4 Added functionality when using markers.
  33. %   31 Oct 2015 - Updates for compatibility with 2015a, Adds minor improvements as per user suggestions
  34. %   07 Nov 2016 - Bug fixes, added functionality for bar plots, added all valid legend locations
  35. %   07 Jun 2017 - Added a quick padding feature that creates additional
  36. %   vertical space between legend entries.


  37. location = 'NorthEast';
  38. boxon = false; legend_h = false; padding = 0;
  39. for i=1:2:length(varargin),
  40.     switch lower(varargin{i})
  41.         case 'location'
  42.             location = varargin{i+1};
  43.             i=i+2;
  44.         case 'boxon'
  45.             boxon = true;
  46.         case 'boxoff'
  47.             boxon = false;
  48.         case 'legend'
  49.             legend_h = varargin{i+1};
  50.             i=i+2;
  51.         case 'object'
  52.             object_h = varargin{i+1};
  53.             i=i+2;
  54.         case 'padding'
  55.             padding = varargin{i+1};
  56.             i=i+2;   
  57.     end
  58. end

  59. if legend_h == false,
  60.     %create the legend
  61.     [legend_h,object_h,plot_h,text_strings] = legend(str);
  62. end

  63. %some variables
  64. numlines = length(str);
  65. numpercolumn = ceil(numlines/numcolumns);

  66. %get old width, new width and scale factor
  67. set(legend_h, 'units', 'normalized');
  68. set(gca, 'units', 'normalized');

  69. pos = get(legend_h, 'position');
  70. width = numcolumns*pos(3);
  71. newheight = (pos(4)/numlines)*numpercolumn;
  72. rescale = pos(3)/width;


  73. %get some old values so we can scale everything later
  74. type = get(object_h(numlines+1), 'type');
  75. switch type,
  76.     case {'line'}
  77.         xdata = get(object_h(numlines+1), 'xdata');
  78.         ydata1 = get(object_h(numlines+1), 'ydata');
  79.         ydata2 = get(object_h(numlines+3), 'ydata');
  80.         
  81.         %we'll use these later to align things appropriately
  82.         sheight     = ydata1(1)-ydata2(1) + (ydata1(1)-ydata2(1))*padding;          % height between data lines
  83.         height      = ydata1(1) + (ydata1(1)-ydata2(1))*padding*2;                    % height of the box. Used to top margin offset
  84.         line_width = (xdata(2)-xdata(1))*rescale;       % rescaled linewidth to match original
  85.         spacer = xdata(1)*rescale;                      % rescaled spacer used for margins
  86.     case {'hggroup'}
  87.         text_pos    = get(object_h(1), 'position');
  88.         child       = get(object_h(numlines+1), 'children');
  89.         vertices_1  = get(child, 'vertices');
  90.         child       = get(object_h(numlines+2), 'children');
  91.         vertices_2  = get(child, 'vertices');
  92.         sheight     = vertices_1(2,2)-vertices_1(1,2);
  93.         height      = vertices_1(2,2);
  94.         line_width  = (vertices_1(3,1)-vertices_1(1,1))*rescale;       % rescaled linewidth to match original
  95.         spacer      = vertices_1(1,2)-vertices_2(2,2);                      % rescaled spacer used for margins
  96.         text_space  = (text_pos(1)-vertices_1(4,1))./numcolumns;      
  97. end


  98. %put the legend on the upper left corner to make initial adjustments easier
  99. % set(gca, 'units', 'pixels');
  100. loci = get(gca, 'position');
  101. set(legend_h, 'position', [loci(1) pos(2) width pos(4)]);

  102. col = -1;
  103. for i=1:numlines,
  104.     if (mod(i,numpercolumn)==1 || (numpercolumn == 1)),
  105.         col = col+1;
  106.     end
  107.    
  108.     if i==1
  109.         linenum = i+numlines;
  110.     else
  111.         if strcmp(type, 'line'),
  112.             linenum = linenum+2;
  113.         else
  114.             linenum = linenum+1;
  115.         end
  116.     end
  117.     labelnum = i;
  118.    
  119.     position = mod(i,numpercolumn);
  120.     if position == 0,
  121.          position = numpercolumn;
  122.     end
  123.    
  124.     switch type,
  125.         case {'line'}
  126.             %realign the labels
  127.             set(object_h(linenum), 'ydata', [(height-(position-1)*sheight) (height-(position-1)*sheight)]);
  128.             set(object_h(linenum), 'xdata', [col/numcolumns+spacer col/numcolumns+spacer+line_width]);
  129.             
  130.             set(object_h(linenum+1), 'ydata', [height-(position-1)*sheight height-(position-1)*sheight]);
  131.             set(object_h(linenum+1), 'xdata', [col/numcolumns+spacer*3.5 col/numcolumns+spacer*3.5]);
  132.             
  133.             set(object_h(labelnum), 'position', [col/numcolumns+spacer*2+line_width height-(position-1)*sheight]);
  134.         case {'hggroup'},
  135.             child = get(object_h(linenum), 'children');
  136.             v = get(child, 'vertices');
  137.             %x-positions
  138.             v([1:2 5],1) = col/numcolumns+spacer;
  139.             v(3:4,1) = col/numcolumns+spacer+line_width;
  140.             % y-positions
  141.             v([1 4 5],2) = (height-(position-1)*sheight-(position-1)*spacer);
  142.             v([2 3], 2) = v(1,2)+sheight;
  143.             set(child, 'vertices', v);
  144.             set(object_h(labelnum), 'position', [v(3,1)+text_space v(1,2)+(v(2,2)-v(1,2))/2 v(3,1)-v(1,1)]);
  145.     end
  146.    
  147. end

  148. %unfortunately, it is not possible to force the box to be smaller than the
  149. %original height, therefore, turn it off and set background color to none
  150. %so that it no longer appears
  151. set(legend_h, 'Color', 'None', 'Box', 'off');

  152. %let's put it where you want it
  153. fig_pos = get(gca, 'position');
  154. pos = get(legend_h, 'position');
  155. padding = 0.01; % padding, in normalized units
  156. % if location is some variation on south, then we need to take into account
  157. % the new height
  158. if strfind(location, 'south'),
  159.     h_diff = pos(4)-newheight;
  160.     pos(4) = newheight;
  161. end
  162. switch lower(location),
  163.     case {'northeast'}
  164.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3)-padding pos(2) pos(3) pos(4)]);
  165.     case {'northwest'}
  166.         set(legend_h, 'position', [pos(1)+padding pos(2) pos(3) pos(4)]);        
  167.     case {'southeast'}
  168.         pos(4) = newheight;
  169.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3)-padding fig_pos(2)-pos(4)/2+pos(4)/4 pos(3) pos(4)]);
  170.     case {'southwest'}
  171.         set(legend_h, 'position', [fig_pos(1)+padding fig_pos(2)-pos(4)/2+pos(4)/4 pos(3) pos(4)]);
  172.     case {'northeastoutside'}
  173.         % need to resize axes to allow legend to fit in figure window
  174.         set(gca, 'position', [fig_pos]-[0 0 pos(3) 0]);
  175.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3) pos(2) pos(3) pos(4)]);
  176.     case {'northwestoutside'}
  177.         % need to resize axes to allow legend to fit in figure window
  178.         set(gca, 'position', [fig_pos]+[pos(3) 0 -pos(3) 0]);
  179.         set(legend_h, 'position', [fig_pos(1)-fig_pos(3)*.1 pos(2) pos(3) pos(4)]); % -10% figurewidth to account for axis labels
  180.     case {'north'}
  181.         % need to resize axes to allow legend to fit in figure window
  182.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2 fig_pos(2)+(fig_pos(4)-pos(4))-padding pos(3) pos(4)]);   
  183.     case {'northoutside'}
  184.         % need to resize axes to allow legend to fit in figure window
  185.         set(gca, 'position', [fig_pos]-[0 0 0 pos(4)]);
  186.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2 fig_pos(2)+(fig_pos(4)-pos(4)) pos(3) pos(4)]);
  187.     case {'south'}
  188.         y_pos = fig_pos(2)-h_diff+pos(4);
  189.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2  y_pos pos(3) pos(4)]);
  190.     case {'southoutside'}
  191.         % need to resize axes to allow legend to fit in figure window
  192.         set(gca, 'position', [fig_pos]-[0 -pos(4) 0 pos(4)]);
  193.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2 fig_pos(2)-pos(4)-pos(3)*0.1 pos(3) pos(4)]);
  194.     case {'eastoutside'}
  195.         % need to resize axes to allow legend to fit in figure window
  196.         set(gca, 'position', [fig_pos]-[0 0 pos(3) 0]);
  197.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3) fig_pos(2)+fig_pos(4)/2-pos(4)/2 pos(3) pos(4)]);
  198.     case {'southeastoutside'}
  199.         % need to resize axes to allow legend to fit in figure window
  200.         set(gca, 'position', [fig_pos]-[0 0 pos(3) 0]);
  201.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3) fig_pos(2)-pos(4)/4 pos(3) pos(4)]);
  202.     case {'westoutside'}
  203.         % need to resize axes to allow legend to fit in figure window
  204.         set(gca, 'position', [fig_pos]+[pos(3) 0 -pos(3) 0]);
  205.         set(legend_h, 'position', [fig_pos(1)-fig_pos(3)*.1 fig_pos(2)+fig_pos(4)/2-pos(4)/2 pos(3) pos(4)]); % -10% figurewidth to account for axis labels   
  206.     case {'southwestoutside'}
  207.         % need to resize axes to allow legend to fit in figure window
  208.         set(gca, 'position', [fig_pos]+[pos(3) 0 -pos(3) 0]);
  209.         set(legend_h, 'position', [fig_pos(1)-fig_pos(3)*.1 fig_pos(2)-pos(4)/4 pos(3) pos(4)]); % -10% figurewidth to account for axis labels        
  210. end

  211. % display box around legend
  212. if boxon,
  213.     drawnow; % make sure everyhting is drawn in place first.
  214. %     set(legend_h, 'units', 'normalized');
  215.     pos = get(legend_h, 'position');
  216.     orgHeight = pos(4);
  217.     pos(4) = (orgHeight/numlines)*numpercolumn;
  218.     pos(2)=pos(2) + orgHeight-pos(4) - pos(4)*0.05;
  219.     pos(1) = pos(1)+pos(1)*0.01;
  220.     annotation('rectangle',pos, 'linewidth', 1)
  221. end

  222. % re-set to normalized so that things scale properly
  223. set(legend_h, 'units', 'normalized');
  224. set(gca, 'units', 'normalized');
复制代码


步骤三:运行第一个程序(Multiplot)
回复此楼

2 条回复


谱宇 发表于 2018-6-10 12:45:36
尝试一下这个程序:

步骤一:将以下程序保存为Multiplot.m
  1. clc
  2. clear all
  3. close all

  4. nLines = 18;
  5. legend_str = cell(nLines,1);

  6. % myColors = distinguishable_colors(nLines);
  7. myColors = jet(nLines);
  8. t = 0:pi/64:3*pi;
  9. dPhi = pi/16;
  10. lineStyles = {'-', '--', ':', '-.'};
  11. for ii=1:nLines,
  12.     plot(t,sin(t+dPhi*ii),...
  13.         'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...
  14.         'color', myColors(ii,:),...
  15.         'linewidth',3);
  16.     hold on;
  17.     legend_str{ii} = num2str(ii);
  18. end
  19. axis([0 3*pi -1.15 1.6])
  20. legend(legend_str,'location','NorthWest')

  21. for ii=1:nLines,
  22.     plot(t,sin(t+dPhi*ii),...
  23.         'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...
  24.         'color', myColors(ii,:),...
  25.         'linewidth',3);
  26.     hold on;
  27.     legend_str{ii} = num2str(ii);
  28. end
  29. axis([0 3*pi -1.15 1.6])
  30. columnlegend(6,legend_str,'NorthWest');
复制代码


步骤二:将下面这个程序保存为columnlegend.m
  1. function [legend_h,object_h,plot_h,text_strings] = columnlegend(numcolumns, str, varargin)
  2. %
  3. %   columnlegend creates a legend with a specified number of columns.
  4. %   
  5. %   columnlegend(numcolumns, str, varargin)
  6. %       numcolumns - number of columns in the legend
  7. %       str - cell array of strings for the legend
  8. %
  9. %   Additional Input Options:
  10. %   columnlegend(..., 'Location', loc)
  11. %       loc - location variable for legend, default is 'NorthEast'
  12. %                  possible values: 'NorthWest', 'NorthEast', 'SouthEast', 'SouthWest',
  13. %                                   'NorthOutside', 'SouthOutside',
  14. %                                   'NortheastOutside', 'SoutheastOutside'
  15. %
  16. %   columnlegend(numcolumns, str, 'padding', 0.5); % add 50% vertical padding between legend entries (relative to original height)
  17. %      
  18. %   columnlegend(..., 'boxon')
  19. %   columnlegend(..., 'boxoff')
  20. %        set legend bounding box on/off
  21. %
  22. %   example:
  23. %      plot(bsxfun(@times, [0:9]',[1:10]));
  24. %      columnlegend(3, cellstr(num2str([1:10]')), 'location','northwest');
  25. %
  26. %
  27. %   Author: Simon Henin <shenin@gc.cuny.edu>
  28. %   
  29. %   4/09/2013 - Fixed bug with 3 entries / 3 columns
  30. %   4/09/2013 - Added bounding box option as per @Durga Lal Shrestha (fileexchage)
  31. %   11 May 2010 - 1.2 Add instructions for printing figure with columns
  32. %   08 Feb 2011 - 1.4 Added functionality when using markers.
  33. %   31 Oct 2015 - Updates for compatibility with 2015a, Adds minor improvements as per user suggestions
  34. %   07 Nov 2016 - Bug fixes, added functionality for bar plots, added all valid legend locations
  35. %   07 Jun 2017 - Added a quick padding feature that creates additional
  36. %   vertical space between legend entries.


  37. location = 'NorthEast';
  38. boxon = false; legend_h = false; padding = 0;
  39. for i=1:2:length(varargin),
  40.     switch lower(varargin{i})
  41.         case 'location'
  42.             location = varargin{i+1};
  43.             i=i+2;
  44.         case 'boxon'
  45.             boxon = true;
  46.         case 'boxoff'
  47.             boxon = false;
  48.         case 'legend'
  49.             legend_h = varargin{i+1};
  50.             i=i+2;
  51.         case 'object'
  52.             object_h = varargin{i+1};
  53.             i=i+2;
  54.         case 'padding'
  55.             padding = varargin{i+1};
  56.             i=i+2;   
  57.     end
  58. end

  59. if legend_h == false,
  60.     %create the legend
  61.     [legend_h,object_h,plot_h,text_strings] = legend(str);
  62. end

  63. %some variables
  64. numlines = length(str);
  65. numpercolumn = ceil(numlines/numcolumns);

  66. %get old width, new width and scale factor
  67. set(legend_h, 'units', 'normalized');
  68. set(gca, 'units', 'normalized');

  69. pos = get(legend_h, 'position');
  70. width = numcolumns*pos(3);
  71. newheight = (pos(4)/numlines)*numpercolumn;
  72. rescale = pos(3)/width;


  73. %get some old values so we can scale everything later
  74. type = get(object_h(numlines+1), 'type');
  75. switch type,
  76.     case {'line'}
  77.         xdata = get(object_h(numlines+1), 'xdata');
  78.         ydata1 = get(object_h(numlines+1), 'ydata');
  79.         ydata2 = get(object_h(numlines+3), 'ydata');
  80.         
  81.         %we'll use these later to align things appropriately
  82.         sheight     = ydata1(1)-ydata2(1) + (ydata1(1)-ydata2(1))*padding;          % height between data lines
  83.         height      = ydata1(1) + (ydata1(1)-ydata2(1))*padding*2;                    % height of the box. Used to top margin offset
  84.         line_width = (xdata(2)-xdata(1))*rescale;       % rescaled linewidth to match original
  85.         spacer = xdata(1)*rescale;                      % rescaled spacer used for margins
  86.     case {'hggroup'}
  87.         text_pos    = get(object_h(1), 'position');
  88.         child       = get(object_h(numlines+1), 'children');
  89.         vertices_1  = get(child, 'vertices');
  90.         child       = get(object_h(numlines+2), 'children');
  91.         vertices_2  = get(child, 'vertices');
  92.         sheight     = vertices_1(2,2)-vertices_1(1,2);
  93.         height      = vertices_1(2,2);
  94.         line_width  = (vertices_1(3,1)-vertices_1(1,1))*rescale;       % rescaled linewidth to match original
  95.         spacer      = vertices_1(1,2)-vertices_2(2,2);                      % rescaled spacer used for margins
  96.         text_space  = (text_pos(1)-vertices_1(4,1))./numcolumns;      
  97. end


  98. %put the legend on the upper left corner to make initial adjustments easier
  99. % set(gca, 'units', 'pixels');
  100. loci = get(gca, 'position');
  101. set(legend_h, 'position', [loci(1) pos(2) width pos(4)]);

  102. col = -1;
  103. for i=1:numlines,
  104.     if (mod(i,numpercolumn)==1 || (numpercolumn == 1)),
  105.         col = col+1;
  106.     end
  107.    
  108.     if i==1
  109.         linenum = i+numlines;
  110.     else
  111.         if strcmp(type, 'line'),
  112.             linenum = linenum+2;
  113.         else
  114.             linenum = linenum+1;
  115.         end
  116.     end
  117.     labelnum = i;
  118.    
  119.     position = mod(i,numpercolumn);
  120.     if position == 0,
  121.          position = numpercolumn;
  122.     end
  123.    
  124.     switch type,
  125.         case {'line'}
  126.             %realign the labels
  127.             set(object_h(linenum), 'ydata', [(height-(position-1)*sheight) (height-(position-1)*sheight)]);
  128.             set(object_h(linenum), 'xdata', [col/numcolumns+spacer col/numcolumns+spacer+line_width]);
  129.             
  130.             set(object_h(linenum+1), 'ydata', [height-(position-1)*sheight height-(position-1)*sheight]);
  131.             set(object_h(linenum+1), 'xdata', [col/numcolumns+spacer*3.5 col/numcolumns+spacer*3.5]);
  132.             
  133.             set(object_h(labelnum), 'position', [col/numcolumns+spacer*2+line_width height-(position-1)*sheight]);
  134.         case {'hggroup'},
  135.             child = get(object_h(linenum), 'children');
  136.             v = get(child, 'vertices');
  137.             %x-positions
  138.             v([1:2 5],1) = col/numcolumns+spacer;
  139.             v(3:4,1) = col/numcolumns+spacer+line_width;
  140.             % y-positions
  141.             v([1 4 5],2) = (height-(position-1)*sheight-(position-1)*spacer);
  142.             v([2 3], 2) = v(1,2)+sheight;
  143.             set(child, 'vertices', v);
  144.             set(object_h(labelnum), 'position', [v(3,1)+text_space v(1,2)+(v(2,2)-v(1,2))/2 v(3,1)-v(1,1)]);
  145.     end
  146.    
  147. end

  148. %unfortunately, it is not possible to force the box to be smaller than the
  149. %original height, therefore, turn it off and set background color to none
  150. %so that it no longer appears
  151. set(legend_h, 'Color', 'None', 'Box', 'off');

  152. %let's put it where you want it
  153. fig_pos = get(gca, 'position');
  154. pos = get(legend_h, 'position');
  155. padding = 0.01; % padding, in normalized units
  156. % if location is some variation on south, then we need to take into account
  157. % the new height
  158. if strfind(location, 'south'),
  159.     h_diff = pos(4)-newheight;
  160.     pos(4) = newheight;
  161. end
  162. switch lower(location),
  163.     case {'northeast'}
  164.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3)-padding pos(2) pos(3) pos(4)]);
  165.     case {'northwest'}
  166.         set(legend_h, 'position', [pos(1)+padding pos(2) pos(3) pos(4)]);        
  167.     case {'southeast'}
  168.         pos(4) = newheight;
  169.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3)-padding fig_pos(2)-pos(4)/2+pos(4)/4 pos(3) pos(4)]);
  170.     case {'southwest'}
  171.         set(legend_h, 'position', [fig_pos(1)+padding fig_pos(2)-pos(4)/2+pos(4)/4 pos(3) pos(4)]);
  172.     case {'northeastoutside'}
  173.         % need to resize axes to allow legend to fit in figure window
  174.         set(gca, 'position', [fig_pos]-[0 0 pos(3) 0]);
  175.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3) pos(2) pos(3) pos(4)]);
  176.     case {'northwestoutside'}
  177.         % need to resize axes to allow legend to fit in figure window
  178.         set(gca, 'position', [fig_pos]+[pos(3) 0 -pos(3) 0]);
  179.         set(legend_h, 'position', [fig_pos(1)-fig_pos(3)*.1 pos(2) pos(3) pos(4)]); % -10% figurewidth to account for axis labels
  180.     case {'north'}
  181.         % need to resize axes to allow legend to fit in figure window
  182.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2 fig_pos(2)+(fig_pos(4)-pos(4))-padding pos(3) pos(4)]);   
  183.     case {'northoutside'}
  184.         % need to resize axes to allow legend to fit in figure window
  185.         set(gca, 'position', [fig_pos]-[0 0 0 pos(4)]);
  186.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2 fig_pos(2)+(fig_pos(4)-pos(4)) pos(3) pos(4)]);
  187.     case {'south'}
  188.         y_pos = fig_pos(2)-h_diff+pos(4);
  189.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2  y_pos pos(3) pos(4)]);
  190.     case {'southoutside'}
  191.         % need to resize axes to allow legend to fit in figure window
  192.         set(gca, 'position', [fig_pos]-[0 -pos(4) 0 pos(4)]);
  193.         set(legend_h, 'position', [fig_pos(1)+fig_pos(3)/2-pos(3)/2 fig_pos(2)-pos(4)-pos(3)*0.1 pos(3) pos(4)]);
  194.     case {'eastoutside'}
  195.         % need to resize axes to allow legend to fit in figure window
  196.         set(gca, 'position', [fig_pos]-[0 0 pos(3) 0]);
  197.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3) fig_pos(2)+fig_pos(4)/2-pos(4)/2 pos(3) pos(4)]);
  198.     case {'southeastoutside'}
  199.         % need to resize axes to allow legend to fit in figure window
  200.         set(gca, 'position', [fig_pos]-[0 0 pos(3) 0]);
  201.         set(legend_h, 'position', [pos(1)+fig_pos(3)-pos(3) fig_pos(2)-pos(4)/4 pos(3) pos(4)]);
  202.     case {'westoutside'}
  203.         % need to resize axes to allow legend to fit in figure window
  204.         set(gca, 'position', [fig_pos]+[pos(3) 0 -pos(3) 0]);
  205.         set(legend_h, 'position', [fig_pos(1)-fig_pos(3)*.1 fig_pos(2)+fig_pos(4)/2-pos(4)/2 pos(3) pos(4)]); % -10% figurewidth to account for axis labels   
  206.     case {'southwestoutside'}
  207.         % need to resize axes to allow legend to fit in figure window
  208.         set(gca, 'position', [fig_pos]+[pos(3) 0 -pos(3) 0]);
  209.         set(legend_h, 'position', [fig_pos(1)-fig_pos(3)*.1 fig_pos(2)-pos(4)/4 pos(3) pos(4)]); % -10% figurewidth to account for axis labels        
  210. end

  211. % display box around legend
  212. if boxon,
  213.     drawnow; % make sure everyhting is drawn in place first.
  214. %     set(legend_h, 'units', 'normalized');
  215.     pos = get(legend_h, 'position');
  216.     orgHeight = pos(4);
  217.     pos(4) = (orgHeight/numlines)*numpercolumn;
  218.     pos(2)=pos(2) + orgHeight-pos(4) - pos(4)*0.05;
  219.     pos(1) = pos(1)+pos(1)*0.01;
  220.     annotation('rectangle',pos, 'linewidth', 1)
  221. end

  222. % re-set to normalized so that things scale properly
  223. set(legend_h, 'units', 'normalized');
  224. set(gca, 'units', 'normalized');
复制代码


步骤三:运行第一个程序(Multiplot)
回复此楼

shmilya 发表于 2018-6-10 19:01:59
谱宇 发表于 2018-6-10 12:45
尝试一下这个程序:

步骤一:将以下程序保存为Multiplot.m

太有用 谢谢:victory:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

相关帖子
相关文章
热门教程
站长推荐
快速回复 返回顶部 返回列表