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

Skip to content

Commit aa413a8

Browse files
authored
Merge pull request #19496 from tacaswell/fix_3D_autoadd
MNT: Restore auto-adding Axes3D to their parent figure on init
2 parents 266be12 + 7b27429 commit aa413a8

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
Axes3D no longer adds itself to figure
2-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3-
4-
New `.Axes3D` objects previously added themselves to figures when they were
5-
created, which lead to them being added twice if
6-
``fig.add_subplot(111, projection='3d')`` was called. Now ``ax = Axes3d(fig)``
7-
will need to be explicitly added to the figure with ``fig.add_axes(ax)``, as
8-
also needs to be done for normal `.axes.Axes`.
1+
Axes3D automatically adding self to Figure is deprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
New `.Axes3D` objects previously added themselves to figures when they
5+
were created, unlike all other Axes classes, which lead to them being
6+
added twice if ``fig.add_subplot(111, projection='3d')`` was called.
7+
8+
This behavior is now deprecated and will warn. The new keyword argument
9+
*auto_add_to_figure* controls the behavior and can be used to suppress the
10+
warning. The default value will change to False in mpl3.5, and any
11+
non-False value will be an error in mpl3.6.
12+
13+
In the future, `.Axes3D` will need to be explicitly added to the
14+
figure ::
15+
16+
fig = Figure()
17+
# create Axes3D
18+
ax = Axes3d(fig)
19+
# add to Figure
20+
fig.add_axes(ax)
21+
22+
as needs to be done for other `.axes.Axes` sub-classes. Or, a 3-d
23+
projection can be made via::
24+
25+
fig.add_subplot(projection='3d')

examples/pyplots/whats_new_99_mplot3d.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
Z = np.sin(R)
1818

1919
fig = plt.figure()
20-
ax = Axes3D(fig)
20+
ax = Axes3D(fig, auto_add_to_figure=False)
21+
fig.add_axes(ax)
2122
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.viridis)
2223

2324
plt.show()

lib/matplotlib/figure.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,8 @@ def _process_projection_requirements(
15161516
raise TypeError(
15171517
f"projection must be a string, None or implement a "
15181518
f"_as_mpl_axes method, not {projection!r}")
1519-
1519+
if projection_class.__name__ == 'Axes3D':
1520+
kwargs.setdefault('auto_add_to_figure', False)
15201521
return projection_class, kwargs
15211522

15221523
def get_default_bbox_extra_artists(self):

lib/matplotlib/tests/test_collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def test_polycollection_close():
393393
[[3., 0.], [3., 1.], [4., 1.], [4., 0.]]]
394394

395395
fig = plt.figure()
396-
ax = fig.add_axes(Axes3D(fig))
396+
ax = fig.add_axes(Axes3D(fig, auto_add_to_figure=False))
397397

398398
colors = ['r', 'g', 'b', 'y', 'k']
399399
zpos = list(range(5))

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ def __init__(
6464
Other axes to share z-limits with.
6565
proj_type : {'persp', 'ortho'}
6666
The projection type, default 'persp'.
67+
auto_add_to_figure : bool, default: True
68+
Prior to Matplotlib 3.4 Axes3D would add themselves
69+
to their host Figure on init. Other Axes class do not
70+
do this.
71+
72+
This behavior is deprecated in 3.4, the default will
73+
change to False in 3.5. The keyword will be undocumented
74+
and a non-False value will be an error in 3.6.
75+
6776
**kwargs
6877
Other optional keyword arguments:
6978
@@ -97,6 +106,8 @@ def __init__(
97106
self._shared_z_axes.join(self, sharez)
98107
self._adjustable = 'datalim'
99108

109+
auto_add_to_figure = kwargs.pop('auto_add_to_figure', True)
110+
100111
super().__init__(
101112
fig, rect, frameon=True, box_aspect=box_aspect, *args, **kwargs
102113
)
@@ -129,6 +140,18 @@ def __init__(
129140
# for bounding box calculations
130141
self.spines[:].set_visible(False)
131142

143+
if auto_add_to_figure:
144+
_api.warn_deprecated(
145+
"3.4", removal="3.6", message="Axes3D(fig) adding itself "
146+
"to the figure is deprecated since %(since)s. "
147+
"Pass the keyword argument auto_add_to_figure=False "
148+
"and use fig.add_axes(ax) to suppress this warning. "
149+
"The default value of auto_add_to_figure will change to "
150+
"False in mpl3.5 and True values will "
151+
"no longer work %(removal)s. This is consistent with "
152+
"other Axes classes.")
153+
fig.add_axes(self)
154+
132155
def set_axis_off(self):
133156
self._axis3don = False
134157
self.stale = True

lib/mpl_toolkits/tests/test_mplot3d.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
from mpl_toolkits.mplot3d import Axes3D, axes3d, proj3d, art3d
77
import matplotlib as mpl
88
from matplotlib.backend_bases import MouseButton
9+
from matplotlib.cbook import MatplotlibDeprecationWarning
910
from matplotlib import cm
1011
from matplotlib import colors as mcolors
1112
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1213
from matplotlib.testing.widgets import mock_event
1314
from matplotlib.collections import LineCollection, PolyCollection
1415
from matplotlib.patches import Circle
16+
1517
import matplotlib.pyplot as plt
1618
import numpy as np
1719

@@ -725,7 +727,7 @@ def test_add_collection3d_zs_scalar():
725727
@mpl3d_image_comparison(['axes3d_labelpad.png'], remove_text=False)
726728
def test_axes3d_labelpad():
727729
fig = plt.figure()
728-
ax = fig.add_axes(Axes3D(fig))
730+
ax = fig.add_axes(Axes3D(fig, auto_add_to_figure=False))
729731
# labelpad respects rcParams
730732
assert ax.xaxis.labelpad == mpl.rcParams['axes.labelpad']
731733
# labelpad can be set in set_label
@@ -1148,7 +1150,8 @@ def test_inverted_cla():
11481150

11491151
def test_ax3d_tickcolour():
11501152
fig = plt.figure()
1151-
ax = Axes3D(fig)
1153+
with pytest.warns(MatplotlibDeprecationWarning):
1154+
ax = Axes3D(fig)
11521155

11531156
ax.tick_params(axis='x', colors='red')
11541157
ax.tick_params(axis='y', colors='red')

0 commit comments

Comments
 (0)