Skip to content

FIX: allow add option for Axes3D(fig) #19413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/mpl_toolkits/tests/test_mplot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down