'Build an :class:`Grid` instance with a grid nrows*ncols
:class:`~matplotlib.axes.Axes` in
:class:`~matplotlib.figure.Figure` *fig* with
*rect=[left, bottom, width, height]* (in
:class:`~matplotlib.figure.Figure` coordinates) or
the subplot position code (e.g., "121").
Optional keyword arguments:
Keyword           Default   Description
direction         "row"     [ "row" | "column" ]
axes_pad          0.02      float| pad between axes given in inches
or tuple-like of floats,
(horizontal padding, vertical padding)
add_all           True      [ True | False ]
share_all         False     [ True | False ]
share_x           True      [ True | False ]
share_y           True      [ True | False ]
label_mode        "L"       [ "L" | "1" | "all" ]
axes_class        None      a type object which must be a subclass
of :class:`~matplotlib.axes.Axes`'

def __init__(self, fig, rect, nrows_ncols, ngrids=None, direction=u'row', axes_pad=0.02, add_all=True, share_all=False, share_x=True, share_y=True, label_mode=u'L', axes_class=None):

    (self._nrows, self._ncols) = nrows_ncols
    if (ngrids is None):
        ngrids = (self._nrows * self._ncols)
    elif ((ngrids > (self._nrows * self._ncols)) or (ngrids <= 0)):
        raise Exception(u'')
    self.ngrids = ngrids
    self._init_axes_pad(axes_pad)
    if (direction not in [u'column', u'row']):
        raise Exception(u'')
    self._direction = direction
    if (axes_class is None):
        axes_class = self._defaultLocatableAxesClass
        axes_class_args = {}
    elif ((type(axes_class) == type) and issubclass(axes_class, self._defaultLocatableAxesClass.Axes)):
        axes_class_args = {}
    else:
        (axes_class, axes_class_args) = axes_class
    self.axes_all = []
    self.axes_column = [[] for _ in range(self._ncols)]
    self.axes_row = [[] for _ in range(self._nrows)]
    h = []
    v = []
    if (isinstance(rect, six.string_types) or cbook.is_numlike(rect)):
        self._divider = SubplotDivider(fig, rect, horizontal=h, vertical=v, aspect=False)
    elif isinstance(rect, SubplotSpec):
        self._divider = SubplotDivider(fig, rect, horizontal=h, vertical=v, aspect=False)
    elif (len(rect) == 3):
        kw = dict(horizontal=h, vertical=v, aspect=False)
        self._divider = SubplotDivider(fig, *rect, **kw)
    elif (len(rect) == 4):
        self._divider = Divider(fig, rect, horizontal=h, vertical=v, aspect=False)
    else:
        raise Exception(u'')
    rect = self._divider.get_position()
    self._column_refax = [None for _ in range(self._ncols)]
    self._row_refax = [None for _ in range(self._nrows)]
    self._refax = None
    for i in range(self.ngrids):
        (col, row) = self._get_col_row(i)
        if share_all:
            sharex = self._refax
            sharey = self._refax
        else:
            if share_x:
                sharex = self._column_refax[col]
            else:
                sharex = None
            if share_y:
                sharey = self._row_refax[row]
            else:
                sharey = None
        ax = axes_class(fig, rect, sharex=sharex, sharey=sharey, **axes_class_args)
        if share_all:
            if (self._refax is None):
                self._refax = ax
        else:
            if (sharex is None):
                self._column_refax[col] = ax
            if (sharey is None):
                self._row_refax[row] = ax
        self.axes_all.append(ax)
        self.axes_column[col].append(ax)
        self.axes_row[row].append(ax)
    self.axes_llc = self.axes_column[0][(-1)]
    self._update_locators()
    if add_all:
        for ax in self.axes_all:
            fig.add_axes(ax)
    self.set_label_mode(label_mode)
