|
18 | 18 | from matplotlib import rcParams
|
19 | 19 | from matplotlib import docstring, projections
|
20 | 20 | from matplotlib import __version__ as _mpl_version
|
21 |
| -import matplotlib.gridspec as gridspec |
22 | 21 |
|
23 | 22 | import matplotlib.artist as martist
|
24 | 23 | from matplotlib.artist import Artist, allow_rasterization
|
|
30 | 29 |
|
31 | 30 | from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
|
32 | 31 | from matplotlib.blocking_input import BlockingMouseInput, BlockingKeyMouseInput
|
33 |
| -from matplotlib.gridspec import GridSpec |
| 32 | +from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec |
34 | 33 | import matplotlib.legend as mlegend
|
35 | 34 | from matplotlib.patches import Rectangle
|
36 | 35 | from matplotlib.text import Text
|
@@ -1617,25 +1616,47 @@ def build_grid(self, layout, subplot_kw=None, gridspec_kw=None):
|
1617 | 1616 | """
|
1618 | 1617 | subplot_kw = subplot_kw or {}
|
1619 | 1618 | gridspec_kw = gridspec_kw or {}
|
| 1619 | + |
| 1620 | + def _process_layout(layout): |
| 1621 | + unique_ids = set() |
| 1622 | + nested = {} |
| 1623 | + for j, row in enumerate(layout): |
| 1624 | + for k, v in enumerate(row): |
| 1625 | + if isinstance(v, str): |
| 1626 | + unique_ids.add(v) |
| 1627 | + else: |
| 1628 | + nested[(j, k)] = v |
| 1629 | + |
| 1630 | + return unique_ids, nested |
| 1631 | + |
| 1632 | + def _do_layout(gs, layout, unique_ids, nested): |
| 1633 | + rows, cols = layout.shape |
| 1634 | + covered = np.zeros((rows, cols), dtype=bool) |
| 1635 | + output = dict() |
| 1636 | + for name in unique_ids: |
| 1637 | + indx = np.stack(np.where(layout == name)).T |
| 1638 | + start_row, start_col = np.min(indx, axis=0) |
| 1639 | + end_row, end_col = np.max(indx, axis=0) + 1 |
| 1640 | + slc = (slice(start_row, end_row), slice(start_col, end_col)) |
| 1641 | + if not np.all(covered[slc] == False): |
| 1642 | + raise ValueError |
| 1643 | + covered[slc] = True |
| 1644 | + output[name] = self.add_subplot(gs[slc], **subplot_kw) |
| 1645 | + |
| 1646 | + for (j, k), layout in nested.items(): |
| 1647 | + layout = np.asarray(layout) |
| 1648 | + rows, cols = layout.shape |
| 1649 | + gs_n = GridSpecFromSubplotSpec(rows, cols, gs[j, k]) |
| 1650 | + nested_output = _do_layout(gs_n, layout, *_process_layout(layout)) |
| 1651 | + |
| 1652 | + output.update(nested_output) |
| 1653 | + return output |
| 1654 | + |
1620 | 1655 | layout = np.asarray(layout)
|
1621 | 1656 | rows, cols = layout.shape
|
1622 |
| - unique_ids = set(layout.ravel()) |
1623 |
| - |
1624 |
| - gs = gridspec.GridSpec(rows, cols, figure=self, **gridspec_kw) |
1625 |
| - |
1626 |
| - covered = np.zeros((rows, cols), dtype=bool) |
1627 |
| - output = dict() |
1628 |
| - for name in unique_ids: |
1629 |
| - indx = np.stack(np.where(layout == name)).T |
1630 |
| - start_row, start_col = np.min(indx, axis=0) |
1631 |
| - end_row, end_col = np.max(indx, axis=0) + 1 |
1632 |
| - slc = (slice(start_row, end_row), slice(start_col, end_col)) |
1633 |
| - if not np.all(covered[slc] == False): |
1634 |
| - raise ValueError |
1635 |
| - covered[slc] = True |
1636 |
| - output[name] = self.add_subplot(gs[slc], **subplot_kw) |
1637 |
| - |
1638 |
| - return output |
| 1657 | + gs = GridSpec(rows, cols, figure=self, **gridspec_kw) |
| 1658 | + return _do_layout(gs, layout, *_process_layout(layout)) |
| 1659 | + |
1639 | 1660 |
|
1640 | 1661 | def delaxes(self, ax):
|
1641 | 1662 | """
|
|
0 commit comments