Home > atmlab > graphs > add_plot_row.m

add_plot_row

PURPOSE ^

ADD_PLOT_ROW Adds a row of plots to a figure.

SYNOPSIS ^

function h = add_plot_row(height,widths,varargin)

DESCRIPTION ^

 ADD_PLOT_ROW   Adds a row of plots to a figure.

    This function works in a similar way as *subplot*, but this function
    can be called repeatedly, and each row of plots can have a different
    number of plots.

    The size allocated for the plots is specified by giving lengths with
    respect to the vertical size of the figure area. This is valid both
    for widths and height. This means that if the width and height are set
    to the same value, the plot will be square. All plots have the same height
    but the width can differ. Note that the size given does not include the
    space used for axis numbering and label text, it includes only the actual
    plot area. This means that if there shall be any space left for y-labels
    the plot widths cannot add up to 1.

    The plots can either be flushed to the right (flush='r'), flushed to
    the left (flush='l'), or be centered (flush='c'). For left and right
    flushing, 10% of the figure width at the end of the flushing. 
    The horisontal position of the plots can be fine tuned by the optional
    arguments *hspacefac* and *hshift*.

    For normal plotting with the y-axis to the left, right flushing should
    be the standard choice, and it is also default.

    Some examples:
       h1 = add_plot_row(0.3,[0.25 0.4 0.25]);
       h2 = add_plot_row(0.3,1.1);
    Make sure that the figure window has sufficient height for both plot rows
    before trying this example. The width must be increased before creating
    the first rows the widths set are normalised to the size, and the size of
    the plots will change if the size of the figure is changed.

 FORMAT   h = add_plot_row(height,widths,vspacing,hspacing,flush,hshift)
        
 OUT   h          Handles to the created plots (axes).
 IN    height     Height of plots. This length is given in fractions of the 
                  vertical size. 
       widths     Widths of each plot as a vector. This lengths are given in
                  fractions of the vertical size. 
 OPT   vspacing   Vertical spacing to plots above, or to the top of figure
                  area. This length is given in fractions of the vertical
                  size. Default is 0.1.
       hspacing   Horisontal spacing of plots in row,. This length is given 
                  in fractions of the vertical size. Default is 0.1.
       flush      Flushing of plots. See further above. Default is 'r'.
       hshift     Horisontal shift of plots. Same unit as above. Default is 0.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

add_plot_row.m

SOURCE CODE ^

0001 % ADD_PLOT_ROW   Adds a row of plots to a figure.
0002 %
0003 %    This function works in a similar way as *subplot*, but this function
0004 %    can be called repeatedly, and each row of plots can have a different
0005 %    number of plots.
0006 %
0007 %    The size allocated for the plots is specified by giving lengths with
0008 %    respect to the vertical size of the figure area. This is valid both
0009 %    for widths and height. This means that if the width and height are set
0010 %    to the same value, the plot will be square. All plots have the same height
0011 %    but the width can differ. Note that the size given does not include the
0012 %    space used for axis numbering and label text, it includes only the actual
0013 %    plot area. This means that if there shall be any space left for y-labels
0014 %    the plot widths cannot add up to 1.
0015 %
0016 %    The plots can either be flushed to the right (flush='r'), flushed to
0017 %    the left (flush='l'), or be centered (flush='c'). For left and right
0018 %    flushing, 10% of the figure width at the end of the flushing.
0019 %    The horisontal position of the plots can be fine tuned by the optional
0020 %    arguments *hspacefac* and *hshift*.
0021 %
0022 %    For normal plotting with the y-axis to the left, right flushing should
0023 %    be the standard choice, and it is also default.
0024 %
0025 %    Some examples:
0026 %       h1 = add_plot_row(0.3,[0.25 0.4 0.25]);
0027 %       h2 = add_plot_row(0.3,1.1);
0028 %    Make sure that the figure window has sufficient height for both plot rows
0029 %    before trying this example. The width must be increased before creating
0030 %    the first rows the widths set are normalised to the size, and the size of
0031 %    the plots will change if the size of the figure is changed.
0032 %
0033 % FORMAT   h = add_plot_row(height,widths,vspacing,hspacing,flush,hshift)
0034 %
0035 % OUT   h          Handles to the created plots (axes).
0036 % IN    height     Height of plots. This length is given in fractions of the
0037 %                  vertical size.
0038 %       widths     Widths of each plot as a vector. This lengths are given in
0039 %                  fractions of the vertical size.
0040 % OPT   vspacing   Vertical spacing to plots above, or to the top of figure
0041 %                  area. This length is given in fractions of the vertical
0042 %                  size. Default is 0.1.
0043 %       hspacing   Horisontal spacing of plots in row,. This length is given
0044 %                  in fractions of the vertical size. Default is 0.1.
0045 %       flush      Flushing of plots. See further above. Default is 'r'.
0046 %       hshift     Horisontal shift of plots. Same unit as above. Default is 0.
0047 
0048 % 2002-12-11   Created by Patrick Eriksson.
0049 
0050 
0051 function h = add_plot_row(height,widths,varargin)
0052 %
0053 [vspacing,hspacing,flush,hshift] = optargs( varargin, { 0.1, 0.1, 'r', 0 } );
0054 
0055   
0056 %=== Basic check of input
0057 %
0058 if ~isscalar( height )
0059   error(' The argument *height* must be a scalar.');
0060 end
0061 %
0062 if ~isvector( widths )
0063   error(' The argument *width* must be a vector.');
0064 end
0065 %
0066 if ~isscalar( vspacing )
0067   error(' The argument *vspacing* must be a scalar.');
0068 end
0069 %
0070 if  ~isscalar( hspacing )
0071   error( 'The argument *hspacing* must be a scalar.');
0072 end
0073 %
0074 if  ~( strcmp(flush,'l') | strcmp(flush,'c') | strcmp(flush,'r') )
0075   error('The argument *flush* must be ''l'', ''c'' or ''r''.');
0076 end 
0077 %
0078 if ~isscalar( hshift )
0079   error(' The argument *hshift* must be a scalar.');
0080 end
0081 
0082 
0083 
0084 %=== Handle to figure
0085 %
0086 hf = gcf;
0087 
0088 
0089 %=== Convert horisontal sizes.
0090 %
0091 %- The case of unit='norm' forces the conversion to be done in a more tricky
0092 %- way, than required for other units.
0093 %
0094 %- Save unit and change to cm
0095 unit1 = get( hf, 'unit' );
0096 set( hf, 'unit', 'ce' );
0097 %
0098 pos      = get( hf, 'Position' );
0099 widths   = widths * pos(4) / pos(3);
0100 hspacing = hspacing * pos(4) / pos(3);
0101 hshift   = hshift * pos(4) / pos(3);
0102 %
0103 %- Reset unit
0104 set( hf, 'unit', unit1 );
0105 
0106 
0107 
0108 
0109 %=== Check at what height the plot row shall be placed
0110 %
0111 %- Handle to childrens of the figure
0112 hc = get( hf, 'Children' );
0113 %
0114 if isempty( hc )
0115   vbase = 1;
0116 else
0117   %- Loop children and look at what height they start
0118   vbase = pos(2) + pos(4);
0119   for ic = 1 : length(hc)
0120     cpos = get( hc(ic), 'position' );
0121     if cpos(2) < vbase
0122       vbase = cpos(2);
0123     end 
0124   end
0125 end
0126 %
0127 vbase = vbase - vspacing - height;
0128 %
0129 if vbase < 0
0130   error('The given height is larger than free vertical space.');
0131 end
0132 
0133 
0134 
0135 
0136 %=== Place the axes
0137 %
0138 nplots = length( widths );
0139 %
0140 if flush == 'l'
0141   hbase = 0.1;
0142 elseif flush == 'c'
0143   hbase = 0.5 - (sum(widths) + hspacing*(nplots-1))/2;
0144 else
0145   hbase = 0.9 - (sum(widths) + hspacing*(nplots-1));
0146 end
0147 %
0148 hbase = hbase + hshift;
0149 
0150 
0151 h     = zeros( nplots, 1 );
0152 %
0153 for ip = 1 : nplots
0154   h(ip) = axes( 'position', [hbase vbase widths(ip) height] );
0155   hbase = hbase + widths(ip) + hspacing;
0156 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005