diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index b85275bce970..73f211a6998a 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -645,6 +645,8 @@ def __init__(self, fig, rect, if yscale: self.set_yscale(yscale) + # remove when Axis3d deprecation expires and this kwarg is removed: + kwargs.pop('add', None) self.update(kwargs) for name, axis in self._get_axis_map().items(): diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index f4e5183e3838..2da7a460e735 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -579,6 +579,8 @@ def add_axes(self, *args, **kwargs): projection_class, kwargs = self._process_projection_requirements( *args, **kwargs) + # remove this when deprecation for Axes3d(add=True) ends: + kwargs['add'] = False # create the new axes using the axes class given a = projection_class(self, rect, **kwargs) return self._add_axes_internal(a) @@ -708,6 +710,10 @@ def add_subplot(self, *args, **kwargs): args = tuple(map(int, str(args[0]))) projection_class, kwargs = self._process_projection_requirements( *args, **kwargs) + + # remove this when deprecation for Axes3d(add=True) ends: + kwargs['add'] = False + ax = subplot_class_factory(projection_class)(self, *args, **kwargs) return self._add_axes_internal(ax) diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index 9fe73e3b892a..3523645ee999 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -391,7 +391,7 @@ def test_polycollection_close(): [[3., 0.], [3., 1.], [4., 1.], [4., 0.]]] fig = plt.figure() - ax = fig.add_axes(Axes3D(fig)) + ax = fig.add_axes(Axes3D(fig, add=False)) colors = ['r', 'g', 'b', 'y', 'k'] zpos = list(range(5)) diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index 14d2b9f4a89d..75af7f07e4ca 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -97,6 +97,8 @@ def __init__( self._shared_z_axes.join(self, sharez) self._adjustable = 'datalim' + add = kwargs.pop("add", True) + super().__init__( fig, rect, frameon=True, box_aspect=box_aspect, *args, **kwargs ) @@ -125,6 +127,14 @@ def __init__( pseudo_bbox = self.transLimits.inverted().transform([(0, 0), (1, 1)]) self._pseudo_w, self._pseudo_h = pseudo_bbox[1] - pseudo_bbox[0] + if add: + _api.warn_deprecated( + "3.4", message="Axes3D(fig) adding itself " + "to the figure is deprecated since %(since)s and will " + "no longer work %(removal)s; this is consistent with " + "other axes classes. Use fig.add_subplot(projection='3d')") + self.figure.add_axes(self) + # mplot3d currently manages its own spines and needs these turned off # for bounding box calculations self.spines[:].set_visible(False) diff --git a/lib/mpl_toolkits/tests/test_mplot3d.py b/lib/mpl_toolkits/tests/test_mplot3d.py index c7d1818b9c40..66c61be873e3 100644 --- a/lib/mpl_toolkits/tests/test_mplot3d.py +++ b/lib/mpl_toolkits/tests/test_mplot3d.py @@ -725,7 +725,8 @@ def test_add_collection3d_zs_scalar(): @mpl3d_image_comparison(['axes3d_labelpad.png'], remove_text=False) def test_axes3d_labelpad(): fig = plt.figure() - ax = fig.add_axes(Axes3D(fig)) + ax = Axes3D(fig, add=False) + fig.add_axes(ax) # labelpad respects rcParams assert ax.xaxis.labelpad == mpl.rcParams['axes.labelpad'] # labelpad can be set in set_label @@ -1132,7 +1133,7 @@ def test_inverted_cla(): def test_ax3d_tickcolour(): fig = plt.figure() - ax = Axes3D(fig) + ax = fig.add_subplot(projection="3d") ax.tick_params(axis='x', colors='red') ax.tick_params(axis='y', colors='red')