Skip to content

Commit 101a2d5

Browse files
committed
enable sca on subfigure axes
1 parent 357276b commit 101a2d5

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

lib/matplotlib/pyplot.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
from matplotlib import cbook
6060
from matplotlib import _docstring
6161
from matplotlib.backend_bases import FigureCanvasBase, MouseButton
62-
from matplotlib.figure import Figure, figaspect
62+
from matplotlib.figure import Figure, FigureBase, figaspect
6363
from matplotlib.gridspec import GridSpec, SubplotSpec
6464
from matplotlib import rcParams, rcParamsDefault, get_backend, rcParamsOrig
6565
from matplotlib.rcsetup import interactive_bk as _interactive_bk
@@ -690,7 +690,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
690690
691691
Parameters
692692
----------
693-
num : int or str or `.Figure`, optional
693+
num : int or str or `.Figure` or `.SubFigure`, optional
694694
A unique identifier for the figure.
695695
696696
If a figure with that identifier already exists, this figure is made
@@ -702,7 +702,8 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
702702
will be used for the ``Figure.number`` attribute, otherwise, an
703703
auto-generated integer value is used (starting at 1 and incremented
704704
for each new figure). If *num* is a string, the figure label and the
705-
window title is set to this value.
705+
window title is set to this value. If num is a ``SubFigure``, its
706+
parent ``Figure`` is activated.
706707
707708
figsize : (float, float), default: :rc:`figure.figsize`
708709
Width, height in inches.
@@ -753,11 +754,11 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
753754
`~matplotlib.rcParams` defines the default values, which can be modified
754755
in the matplotlibrc file.
755756
"""
756-
if isinstance(num, Figure):
757+
if isinstance(num, FigureBase):
757758
if num.canvas.manager is None:
758759
raise ValueError("The passed figure is not managed by pyplot")
759760
_pylab_helpers.Gcf.set_active(num.canvas.manager)
760-
return num
761+
return num.figure
761762

762763
allnums = get_fignums()
763764
next_num = max(allnums) + 1 if allnums else 1

lib/matplotlib/tests/test_pyplot.py

+24
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,27 @@ def test_fallback_position():
343343
axtest = plt.axes([0.2, 0.2, 0.5, 0.5], position=[0.1, 0.1, 0.8, 0.8])
344344
np.testing.assert_allclose(axtest.bbox.get_points(),
345345
axref.bbox.get_points())
346+
347+
348+
def test_set_current_figure_via_subfigure():
349+
fig1 = plt.figure()
350+
subfigs = fig1.subfigures(2)
351+
352+
plt.figure()
353+
assert plt.gcf() != fig1
354+
355+
current = plt.figure(subfigs[1])
356+
assert plt.gcf() == fig1
357+
assert current == fig1
358+
359+
360+
def test_set_current_axes_on_subfigure():
361+
fig = plt.figure()
362+
subfigs = fig.subfigures(2)
363+
364+
ax = subfigs[0].subplots(1, squeeze=True)
365+
subfigs[1].subplots(1, squeeze=True)
366+
367+
assert plt.gca() != ax
368+
plt.sca(ax)
369+
assert plt.gca() == ax

0 commit comments

Comments
 (0)