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

Skip to content

Commit f1938e8

Browse files
committed
MNT: Restore and deprecate auto-adding Axes3D to figure on init
Only warn when directly created by the user, not via our machinery. Closes #18939
1 parent 01d3149 commit f1938e8

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
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.

lib/matplotlib/figure.py

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

15231524
def get_default_bbox_extra_artists(self):

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 23 additions & 1 deletion
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
@@ -96,10 +105,11 @@ def __init__(
96105
if sharez is not None:
97106
self._shared_z_axes.join(self, sharez)
98107
self._adjustable = 'datalim'
99-
108+
auto_add_to_figure = kwargs.pop('auto_add_to_figure', True)
100109
super().__init__(
101110
fig, rect, frameon=True, box_aspect=box_aspect, *args, **kwargs
102111
)
112+
103113
# Disable drawing of axes by base class
104114
super().set_axis_off()
105115
# Enable drawing of axes by Axes3D class
@@ -129,6 +139,18 @@ def __init__(
129139
# for bounding box calculations
130140
self.spines[:].set_visible(False)
131141

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

0 commit comments

Comments
 (0)