8000 Remove figure-level deprecations from 3.3. · matplotlib/matplotlib@be02b50 · GitHub
[go: up one dir, main page]

Skip to content

Commit be02b50

Browse files
committed
Remove figure-level deprecations from 3.3.
1 parent 6939574 commit be02b50

File tree

7 files changed

+76
-108
lines changed

7 files changed

+76
-108
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
``figure.add_axes()`` without arguments
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Calling ``fig.add_axes()`` with no arguments will raise an error. Adding a
4+
free-floating axes needs a position rectangle. If you want a figure-filling
5+
single axes, use ``add_subplot()`` instead.
6+
7+
All parameters of `.Figure.subplots` except *nrows* and *ncols* are now keyword-only
8+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
This avoids typing e.g. ``subplots(1, 1, 1)`` when meaning ``subplot(1, 1, 1)``,
10+
but actually getting ``subplots(1, 1, sharex=1)``.
11+
12+
``add_subplot()`` validates its inputs
13+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
In particular, for ``add_subplot(rows, cols, index)``, all parameters must
15+
be integral. Previously strings and floats were accepted and converted to
16+
int.
17+
18+
``SubplotSpec.get_rows_columns``
19+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
Use the ``GridSpec.nrows``, ``GridSpec.ncols``, ``SubplotSpec.rowspan``, and
21+
``SubplotSpec.colspan`` properties instead.
22+
23+
``autofmt_xdate(which=None)``
24+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25+
Use its more explicit synonym, ``which="major"``, instead.
26+
27+
`.pyplot.tight_layout` parameters are now keyword-only
28+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29+
All parameters of `.pyplot.tight_layout` are now keyword-only, to be consistent
30+
with `.Figure.tight_layout`.

lib/matplotlib/figure.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,7 @@ def autofmt_xdate(
271271
which : {'major', 'minor', 'both'}, default: 'major'
272272
Selects which ticklabels to rotate.
273273
"""
274-
if which is None:
275-
_api.warn_deprecated(
276-
"3.3", message="Support for passing which=None to mean "
277-
"which='major' is deprecated since %(since)s and will be "
278-
"removed %(removal)s.")
274+
_api.check_in_list(['major', 'minor', 'both'], which=which)
279275
allsubplots = all(hasattr(ax, 'get_subplotspec') for ax in self.axes)
280276
if len(self.axes) == 1:
281277
for label in self.axes[0].get_xticklabels(which=which):
@@ -622,12 +618,8 @@ def add_axes(self, *args, **kwargs):
622618
"""
623619

624620
if not len(args) and 'rect' not in kwargs:
625-
_api.warn_deprecated(
626-
"3.3",
627-
message="Calling add_axes() without argument is "
628-
"deprecated since %(since)s and will be removed %(removal)s. "
629-
"You may want to use add_subplot() instead.")
630-
return
621+
raise TypeError(
622+
"add_axes() missing 1 required positional argument: 'rect'")
631623
elif 'rect' in kwargs:
632624
if len(args):
633625
raise TypeError(
@@ -795,8 +787,7 @@ def _add_axes_internal(self, ax, key):
795787
ax.stale_callback = _stale_figure_callback
796788
return ax
797789

798-
@_api.make_keyword_only("3.3", "sharex")
799-
def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
790+
def subplots(self, nrows=1, ncols=1, *, sharex=False, sharey=False,
800791
squeeze=True, subplot_kw=None, gridspec_kw=None):
801792
"""
802793
Add a set of subplots to this figure.

lib/matplotlib/gridspec.py

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ def __init__(self, nrows, ncols, height_ratios=None, width_ratios=None):
4747
"""
4848
if not isinstance(nrows, Integral) or nrows <= 0:
4949
raise ValueError(
50-
f"Number of rows must be a positive integer, not {nrows}")
50+
f"Number of rows must be a positive integer, not {nrows!r}")
5151
if not isinstance(ncols, Integral) or ncols <= 0:
5252
raise ValueError(
53-
f"Number of columns must be a positive integer, not {ncols}")
53+
f"Number of columns must be a positive integer, not {ncols!r}")
5454
self._nrows, self._ncols = nrows, ncols
5555
self.set_height_ratios(height_ratios)
5656
self.set_width_ratios(width_ratios)
@@ -604,51 +604,40 @@ def _from_subplot_args(figure, args):
604604
- a `.SubplotSpec` -- returned as is;
605605
- one or three numbers -- a MATLAB-style subplot specifier.
606606
"""
607-
message = ("Passing non-integers as three-element position "
608-
"specification is deprecated since %(since)s and will be "
609-
"removed %(removal)s.")
610607
if len(args) == 1:
611608
arg, = args
612609
if isinstance(arg, SubplotSpec):
613610
return arg
614-
else:
615-
if not isinstance(arg, Integral):
616-
_api.warn_deprecated("3.3", message=message)
617-
arg = str(arg)
618-
try:
619-
rows, cols, num = map(int, str(arg))
620-
except ValueError:
621-
raise ValueError(
622-
f"Single argument to subplot must be a three-digit "
623-
f"integer, not {arg}") from None
624-
i = j = num
611+
elif not isinstance(arg, Integral):
612+
raise ValueError(
613+
f"Single argument to subplot must be a three-digit "
614+
f"integer, not {arg!r}")
615+
try:
616+
rows, cols, num = map(int, str(arg))
617+
except ValueError:
618+
raise ValueError(
619+
f"Single argument to subplot must be a three-digit "
620+
f"integer, not {arg!r}") from None
625621
elif len(args) == 3:
626622
rows, cols, num = args
627-
if not (isinstance(rows, Integral) and isinstance(cols, Integral)):
628-
_api.warn_deprecated("3.3", message=message)
629-
rows, cols = map(int, [rows, cols])
630-
gs = GridSpec(rows, cols, figure=figure)
631-
if isinstance(num, tuple) and len(num) == 2:
632-
if not all(isinstance(n, Integral) for n in num):
633-
_api.warn_deprecated("3.3", message=message)
634-
i, j = map(int, num)
635-
else:
636-
i, j = num
637-
else:
638-
if not isinstance(num, Integral):
639-
_api.warn_deprecated("3.3", message=message)
640-
num = int(num)
641-
if num < 1 or num > rows*cols:
642-
raise ValueError(
643-
f"num must be 1 <= num <= {rows*cols}, not {num}")
644-
i = j = num
645623
else:
646624
raise TypeError(f"subplot() takes 1 or 3 positional arguments but "
647625
f"{len(args)} were given")
648626

649627
gs = GridSpec._check_gridspec_exists(figure, rows, cols)
650628
if gs is None:
651629
gs = GridSpec(rows, cols, figure=figure)
630+
if isinstance(num, tuple) and len(num) == 2:
631+
if not all(isinstance(n, Integral) for n in num):
632+
raise ValueError(
633+
f"Subplot specifier tuple must contain integers, not {num}"
634+
)
635+
i, j = num
636+
else:
637+
if not isinstance(num, Integral) or num < 1 or num > rows*cols:
638+
raise ValueError(
639+
f"num must be 1 <= num <= {rows*cols}, not {num!r}")
640+
i = j = num
652641
return gs[i-1:j]
653642

654643
# num2 is a property only to handle the case where it is None and someone
@@ -679,18 +668,6 @@ def get_geometry(self):
679668
rows, cols = self.get_gridspec().get_geometry()
680669
return rows, cols, self.num1, self.num2
681670

682-
@_api.deprecated("3.3", alternative="rowspan, colspan")
683-
def get_rows_columns(self):
684-
"""
685-
Return the subplot row and column numbers as a tuple
686-
``(n_rows, n_cols, row_start, row_stop, col_start, col_stop)``.
687-
"""
688-
gridspec = self.get_gridspec()
689-
nrows, ncols = gridspec.get_geometry()
690-
row_start, col_start = divmod(self.num1, ncols)
691-
row_stop, col_stop = divmod(self.num2, ncols)
692-
return nrows, ncols, row_start, row_stop, col_start, col_stop
693-
694671
@property
695672
def rowspan(self):
696673
"""The rows spanned by this subplot, as a `range` object."""

lib/matplotlib/pyplot.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,8 +1289,7 @@ def subplot(*args, **kwargs):
12891289
return ax
12901290

12911291

1292-
@_api.make_keyword_only("3.3", "sharex")
1293-
def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1292+
def subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True,
12941293
subplot_kw=None, gridspec_kw=None, **fig_kw):
12951294
"""
12961295
Create a figure and a set of subplots.
@@ -1633,27 +1632,6 @@ def subplot_tool(targetfig=None):
16331632
return SubplotTool(targetfig, tool_fig)
16341633

16351634

1636-
# After deprecation elapses, this can be autogenerated by boilerplate.py.
1637-
@_api.make_keyword_only("3.3", "pad")
1638-
def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):
1639-
"""
1640-
Adjust the padding between and around subplots.
1641-
1642-
Parameters
1643-
----------
1644-
pad : float, default: 1.08
1645-
Padding between the figure edge and the edges of subplots,
1646-
as a fraction of the font size.
1647-
h_pad, w_pad : float, default: *pa F438 d*
1648-
Padding (height/width) between edges of adjacent subplots,
1649-
as a fraction of the font size.
1650-
rect : tuple (left, bottom, right, top), default: (0, 0, 1, 1)
1651-
A rectangle in normalized figure coordinates into which the whole
1652-
subplots area (including labels) will fit.
1653-
"""
1654-
gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
1655-
1656-
16571635
def box(on=None):
16581636
"""
16591637
Turn the axes box on or off on the current axes.
@@ -2559,6 +2537,12 @@ def suptitle(t, **kwargs):
25592537
return gcf().suptitle(t, **kwargs)
25602538

25612539

2540+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
2541+
@_copy_docstring_and_deprecators(Figure.tight_layout)
2542+
def tight_layout(*, pad=1.08, h_pad=None, w_pad=None, rect=None):
2543+
return gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
2544+
2545+
25622546
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
25632547
@_copy_docstring_and_deprecators(Figure.waitforbuttonpress)
25642548
def waitforbuttonpress(timeout=-1):

lib/matplotlib/tests/test_figure.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from contextlib import nullcontext
21
from datetime import datetime
32
import io
43
from pathlib import Path
@@ -151,8 +150,7 @@ def test_figure_legend():
151150
def test_gca():
152151
fig = plt.figure()
153152

154-
with pytest.warns(MatplotlibDeprecationWarning):
155-
# empty call to add_axes() will throw deprecation warning
153+
with pytest.raises(TypeError):
156154
assert fig.add_axes() is None
157155

158156
ax0 = fig.add_axes([0, 0, 1, 1])
@@ -249,14 +247,14 @@ def test_add_subplot_invalid():
249247
with pytest.raises(TypeError, match='takes 1 or 3 positional arguments '
250248
'but 4 were given'):
251249
fig.add_subplot(1, 2, 3, 4)
252-
with pytest.warns(cbook.MatplotlibDeprecationWarning,
253-
match='Passing non-integers as three-element position '
254-
'specification is deprecated'):
250+
with pytest.raises(ValueError,
251+
match="Number of rows must be a positive integer, "
252+
"not '2'"):
255253
fig.add_subplot('2', 2, 1)
256-
with pytest.warns(cbook.MatplotlibDeprecationWarning,
257-
match='Passing non-integers as three-element position '
258-
'specification is deprecated'):
259-
fig.add_subplot(2.0, 2, 1)
254+
with pytest.raises(ValueError,
255+
match='Number of columns must be a positive integer, '
256+
'not 2.0'):
257+
fig.add_subplot(2, 2.0, 1)
260258
_, ax = plt.subplots()
261259
with pytest.raises(ValueError,
262260
match='The Subplot must have been created in the '
@@ -371,7 +369,7 @@ def test_figaspect():
371369
assert h / w == 1
372370

373371

374-
@pytest.mark.parametrize('which', [None, 'both', 'major', 'minor'])
372+
@pytest.mark.parametrize('which', ['both', 'major', 'minor'])
375373
def test_autofmt_xdate(which):
376374
date = ['3 Jan 2013', '4 Jan 2013', '5 Jan 2013', '6 Jan 2013',
377375
'7 Jan 2013', '8 Jan 2013', '9 Jan 2013', '10 Jan 2013',
@@ -400,11 +398,9 @@ def test_autofmt_xdate(which):
400398
'FixedFormatter should only be used together with FixedLocator')
401399
ax.xaxis.set_minor_formatter(FixedFormatter(minors))
402400

403-
with (pytest.warns(mpl.MatplotlibDeprecationWarning) if which is None else
404-
nullcontext()):
405-
fig.autofmt_xdate(0.2, angle, 'right', which)
401+
fig.autofmt_xdate(0.2, angle, 'right', which)
406402

407-
if which in ('both', 'major', None):
403+
if which in ('both', 'major'):
408404
for label in fig.axes[0].get_xticklabels(False, 'major'):
409405
assert int(label.get_rotation()) == angle
410406

lib/matplotlib/tests/test_subplots.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,6 @@ def test_exceptions():
144144
plt.subplots(2, 2, sharex='blah')
145145
with pytest.raises(ValueError):
146146
plt.subplots(2, 2, sharey='blah')
147-
# We filter warnings in this test which are genuine since
148-
# the point of this test is to ensure that this raises.
149-
with pytest.warns(UserWarning, match='.*sharex argument to subplots'), \
150-
pytest.raises(ValueError):
151-
plt.subplots(2, 2, -1)
152-
with pytest.warns(UserWarning, match='.*sharex argument to subplots'), \
153-
pytest.raises(ValueError):
154-
plt.subplots(2, 2, 0)
155-
with pytest.warns(UserWarning, match='.*sharex argument to subplots'), \
156-
pytest.raises(ValueError):
157-
plt.subplots(2, 2, 5)
158147

159148

160149
@image_comparison(['subplots_offset_text'], remove_text=False)

tools/boilerplate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def boilerplate_gen():
202202
'ginput',
203203
'subplots_adjust',
204204
'suptitle',
205+
'tight_layout',
205206
'waitforbuttonpress',
206207
)
207208

0 commit comments

Comments
 (0)
0