Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 54729db

Browse filesBrowse files
authored
Merge pull request #28363 from saranti/subfigincolumn
flip subfigures axes to match subplots
2 parents 5d6acdf + 3e0902c commit 54729db
Copy full SHA for 54729db

File tree

4 files changed

+43
-3
lines changed
Filter options

4 files changed

+43
-3
lines changed
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Subfigures
2+
~~~~~~~~~~
3+
4+
`.Figure.subfigures` are now added in row-major order to be consistent with
5+
`.Figure.subplots`. The return value of `~.Figure.subfigures` is not changed,
6+
but the order of ``fig.subfigs`` is.
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Subfigures are now added in row-major order
2+
-------------------------------------------
3+
4+
``Figure.subfigures`` are now added in row-major order for API consistency.
5+
6+
7+
.. plot::
8+
:include-source: true
9+
:alt: Example of creating 3 by 3 subfigures.
10+
11+
import matplotlib.pyplot as plt
12+
13+
fig = plt.figure()
14+
subfigs = fig.subfigures(3, 3)
15+
x = np.linspace(0, 10, 100)
16+
17+
for i, sf in enumerate(fig.subfigs):
18+
ax = sf.subplots()
19+
ax.plot(x, np.sin(x + i), label=f'Subfigure {i+1}')
20+
sf.suptitle(f'Subfigure {i+1}')
21+
ax.set_xticks([])
22+
ax.set_yticks([])
23+
plt.show()

‎lib/matplotlib/figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/figure.py
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,9 @@ def subfigures(self, nrows=1, ncols=1, squeeze=True,
15521552
.. note::
15531553
The *subfigure* concept is new in v3.4, and the API is still provisional.
15541554
1555+
.. versionchanged:: 3.10
1556+
subfigures are now added in row-major order.
1557+
15551558
Parameters
15561559
----------
15571560
nrows, ncols : int, default: 1
@@ -1585,9 +1588,9 @@ def subfigures(self, nrows=1, ncols=1, squeeze=True,
15851588
left=0, right=1, bottom=0, top=1)
15861589

15871590
sfarr = np.empty((nrows, ncols), dtype=object)
1588-
for i in range(ncols):
1589-
for j in range(nrows):
1590-
sfarr[j, i] = self.add_subfigure(gs[j, i], **kwargs)
1591+
for i in range(nrows):
1592+
for j in range(ncols):
1593+
sfarr[i, j] = self.add_subfigure(gs[i, j], **kwargs)
15911594

15921595
if self.get_layout_engine() is None and (wspace is not None or
15931596
hspace is not None):

‎lib/matplotlib/tests/test_figure.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_figure.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,3 +1733,11 @@ def test_warn_colorbar_mismatch():
17331733
subfig3_1.colorbar(im3_2) # should not warn
17341734
with pytest.warns(UserWarning, match="different Figure"):
17351735
subfig3_1.colorbar(im4_1)
1736+
1737+
1738+
def test_subfigure_row_order():
1739+
# Test that subfigures are drawn in row-major order.
1740+
fig = plt.figure()
1741+
sf_arr = fig.subfigures(4, 3)
1742+
for a, b in zip(sf_arr.ravel(), fig.subfigs):
1743+
assert a is b

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.