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

Skip to content

Commit c29bc0c

Browse files
committed
FIX: Disallow twinx/twiny on Axes3D
Closes matplotlib#31522. The root cause is that Axes3D inherits a lot of functionality from Axes that does not work for 3D. This is a design flaw we cannot easily fix. With this PR we have at least a reasonable mechanism in place to flag not-supported methods. When we get aware of additional methods, we can easily add them.
1 parent dde0763 commit c29bc0c

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

doc/api/toolkits/mplot3d/axes3d.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ Aliases and deprecated methods
286286
text3D
287287

288288

289-
Other
290-
-----
289+
Other methods
290+
-------------
291291

292292
.. autosummary::
293293
:toctree: ../../_as_gen
@@ -302,16 +302,16 @@ Other
302302

303303
.. currentmodule:: mpl_toolkits.mplot3d
304304

305-
Sample 3D data
306-
--------------
305+
Other
306+
-----
307307

308308
.. autosummary::
309309
:toctree: ../../_as_gen
310310
:template: autosummary.rst
311311
:nosignatures:
312312

313313
axes3d.get_test_data
314-
314+
axes3d.UnsupportedError
315315

316316
.. minigallery:: mpl_toolkits.mplot3d.axes3d.Axes3D
317317
:add-heading:

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
from . import axis3d
6060

6161

62+
class UnsupportedError(RuntimeError):
63+
"""
64+
Raised on Axes3D methods that are inherited from Axes, but not supported by Axes3D.
65+
"""
66+
67+
6268
@_docstring.interpd
6369
@_api.define_aliases({
6470
"xlim": ["xlim3d"], "ylim": ["ylim3d"], "zlim": ["zlim3d"]})
@@ -71,6 +77,11 @@ class Axes3D(Axes):
7177
As a user, you do not instantiate Axes directly, but use Axes creation
7278
methods instead; e.g. from `.pyplot` or `.Figure`:
7379
`~.pyplot.subplots`, `~.pyplot.subplot_mosaic` or `.Figure.add_axes`.
80+
81+
.. note::
82+
83+
Some of the inherited behavior of Axes is not applicable to 3d and will raise
84+
an `.UnsupportedError`.
7485
"""
7586
name = '3d'
7687

@@ -1197,16 +1208,24 @@ def set_zscale(self, value, **kwargs):
11971208
"""
11981209
self._set_axis_scale(self.zaxis, value, **kwargs)
11991210

1200-
def _raise_semilog_not_implemented(self, name, *args, **kwargs):
1201-
raise NotImplementedError(
1202-
f"Axes3D does not support {name}. Use ax.set_xscale/set_yscale/set_zscale "
1203-
"and ax.plot(...) instead."
1211+
def _raise_unsupported(self, name, *args, **kwargs):
1212+
raise UnsupportedError(f"Axes3D does not support '{name}'.")
1213+
1214+
twinx = partialmethod(_raise_unsupported, "twinx")
1215+
twiny = partialmethod(_raise_unsupported, "twiny")
1216+
secondary_xaxis = partialmethod(_raise_unsupported, "secondary_xaxis")
1217+
secondary_yaxis = partialmethod(_raise_unsupported, "secondary_yaxis")
1218+
1219+
def _raise_semilog_unsupported(self, name, *args, **kwargs):
1220+
raise UnsupportedError(
1221+
f"Axes3D does not support '{name}'. "
1222+
"Use ax.set_xscale/set_yscale/set_zscale and ax.plot(...) instead."
12041223
)
12051224

1206-
semilogx = partialmethod(_raise_semilog_not_implemented, "semilogx")
1207-
semilogy = partialmethod(_raise_semilog_not_implemented, "semilogy")
1208-
semilogz = partialmethod(_raise_semilog_not_implemented, "semilogz")
1209-
loglog = partialmethod(_raise_semilog_not_implemented, "loglog")
1225+
semilogx = partialmethod(_raise_semilog_unsupported, "semilogx")
1226+
semilogy = partialmethod(_raise_semilog_unsupported, "semilogy")
1227+
semilogz = partialmethod(_raise_semilog_unsupported, "semilogz")
1228+
loglog = partialmethod(_raise_semilog_unsupported, "loglog")
12101229

12111230
get_zticks = _axis_method_wrapper("zaxis", "get_ticklocs")
12121231
set_zticks = _axis_method_wrapper("zaxis", "set_ticks")

0 commit comments

Comments
 (0)