|
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 |
21 | 22 |
|
22 | 23 | import matplotlib.artist as martist
|
23 | 24 | from matplotlib.artist import Artist, allow_rasterization
|
@@ -1576,6 +1577,66 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
|
1576 | 1577 | # Returned axis array will be always 2-d, even if nrows=ncols=1.
|
1577 | 1578 | return axarr
|
1578 | 1579 |
|
| 1580 | + def build_grid(self, layout, subplot_kw=None, gridspec_kw=None): |
| 1581 | + """ |
| 1582 | + Builds a layout based on an grid of strings |
| 1583 | +
|
| 1584 | + This is a helper function to build complex GridSpec layouts visually |
| 1585 | +
|
| 1586 | + Parameters |
| 1587 | + ---------- |
| 1588 | + layout : list of list of str |
| 1589 | + A visual layout of how you want your Axes to be arranged labeled as strings. |
| 1590 | + For example :: |
| 1591 | +
|
| 1592 | + x = [['A', 'A', 'B'], |
| 1593 | + ['C', 'D', 'B']] |
| 1594 | +
|
| 1595 | + Produces 4 axes: |
| 1596 | +
|
| 1597 | + - 'A' which is 1 row high and spans the first two columns |
| 1598 | + - 'B' which is 2 rows high and is on the right edge |
| 1599 | + - 'C' which in 1 row and 1 column wide in the bottom left |
| 1600 | + - 'D' which in 1 row and 1 column wide in the bottom center |
| 1601 | +
|
| 1602 | + subplot_kw : dict, optional |
| 1603 | + Dict with keywords passed to the |
| 1604 | + :meth:`~matplotlib.figure.Figure.add_subplot` call used to create |
| 1605 | + each subplot. |
| 1606 | +
|
| 1607 | + gridspec_kw : dict, optional |
| 1608 | + Dict with keywords passed to the |
| 1609 | + `~matplotlib.gridspec.GridSpec` constructor used to create |
| 1610 | + the grid the subplots are placed on. |
| 1611 | +
|
| 1612 | + Returns |
| 1613 | + ------- |
| 1614 | + dict[str, Axes] |
| 1615 | + A dictionary mapping the string labels to the new Axes objects. |
| 1616 | +
|
| 1617 | + """ |
| 1618 | + subplot_kw = subplot_kw or {} |
| 1619 | + gridspec_kw = gridspec_kw or {} |
| 1620 | + layout = np.asarray(layout) |
| 1621 | + 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 |
| 1639 | + |
1579 | 1640 | def delaxes(self, ax):
|
1580 | 1641 | """
|
1581 | 1642 | Remove the `~.axes.Axes` *ax* from the figure; update the current axes.
|
|
0 commit comments