Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63e5ba3

Browse files
committed
ENH/WIP: implement the simplest version of subplots from layout
This implements an idea @anntzer and @story645 had in the simplest possible way: trade a 2D array of strings for a dictionary of new Axes.
1 parent 9a24fb7 commit 63e5ba3

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/matplotlib/figure.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from matplotlib import rcParams
1919
from matplotlib import docstring, projections
2020
from matplotlib import __version__ as _mpl_version
21+
import matplotlib.gridspec as gridspec
2122

2223
import matplotlib.artist as martist
2324
from matplotlib.artist import Artist, allow_rasterization
@@ -1576,6 +1577,66 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
15761577
# Returned axis array will be always 2-d, even if nrows=ncols=1.
15771578
return axarr
15781579

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+
15791640
def delaxes(self, ax):
15801641
"""
15811642
Remove the `~.axes.Axes` *ax* from the figure; update the current axes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from matplotlib.testing.decorators import check_figures_equal
2+
import matplotlib.gridspec as gridspec
3+
4+
5+
@check_figures_equal()
6+
def test_label_loc_vertical(fig_test, fig_ref):
7+
x = [["A", "A", "B"], ["C", "D", "B"]]
8+
grid_axes = fig_test.build_grid(x)
9+
10+
for k, ax in grid_axes.items():
11+
ax.set_title(k)
12+
13+
gs = gridspec.GridSpec(2, 3, figure=fig_ref)
14+
axA = fig_ref.add_subplot(gs[:1, :2])
15+
axA.set_title("A")
16+
17+
axB = fig_ref.add_subplot(gs[:, 2])
18+
axB.set_title("B")
19+
20+
axC = fig_ref.add_subplot(gs[1, 0])
21+
axC.set_title("C")
22+
23+
axD = fig_ref.add_subplot(gs[1, 1])
24+
axD.set_title("D")

0 commit comments

Comments
 (0)