8000 Add "standard" Axes wrapper getters/setters for Axis invertedness. by anntzer · Pull Request #29074 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add "standard" Axes wrapper getters/setters for Axis invertedness. #29074

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

Merged
merged 1 commit into from
Nov 12, 2024
Merged
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
4 changes: 4 additions & 0 deletions doc/api/axes_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,12 @@ Axis limits and direction
:template: autosummary.rst
:nosignatures:

Axes.set_xinverted
Axes.get_xinverted
Axes.invert_xaxis
Axes.xaxis_inverted
Axes.set_yinverted
Axes.get_yinverted
Axes.invert_yaxis
Axes.yaxis_inverted

Expand Down
6 changes: 6 additions & 0 deletions doc/api/toolkits/mplot3d/axes3d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,16 @@ Axis limits and direction
get_zlim
set_zlim
get_w_lims
get_xinverted
set_xinverted
invert_xaxis
xaxis_inverted
get_yinverted
set_yinverted
invert_yaxis
yaxis_inverted
get_zinverted
set_zinverted
invert_zaxis
zaxis_inverted
get_xbound
Expand Down
9 changes: 9 additions & 0 deletions doc/users/next_whats_new/axis_inversion.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Standard getters/setters for axis inversion state
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Whether an axis is inverted can now be queried and set using the `.axes.Axes`
getters `~.Axes.get_xinverted`/`~.Axes.get_yinverted` and setters
`~.Axes.set_xinverted`/`~.Axes.set_yinverted`.

The previously existing methods (`.Axes.xaxis_inverted`, `.Axes.invert_xaxis`)
are now discouraged (but not deprecated) due to their non-standard naming and
behavior.
35 changes: 33 additions & 2 deletions lib/matplotlib/axes/_base.py
6855
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from numbers import Real
from operator import attrgetter
import re
import textwrap
import types

import numpy as np
Expand Down Expand Up @@ -3603,15 +3604,30 @@ def invert_xaxis(self):
"""
Invert the x-axis.

.. admonition:: Discouraged

The use of this method is discouraged.
Use `.Axes.set_xinverted` instead.

See Also
--------
xaxis_inverted
get_xinverted
get_xlim, set_xlim
get_xbound, set_xbound
"""
self.xaxis.set_inverted(not self.xaxis.get_inverted())

set_xinverted = _axis_method_wrapper("xaxis", "set_inverted")
get_xinverted = _axis_method_wrapper("xaxis", "get_inverted")
xaxis_inverted = _axis_method_wrapper("xaxis", "get_inverted")
if xaxis_inverted.__doc__:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__doc__ can be unset in "optimized" mode (https://docs.python.org/3/using/cmdline.html#cmdoption-OO). There's already a few other places in the codebase with the same check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, still not following what this does or why... Is this because we are building __doc__ dynamically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in optimized mode the following line `xaxis_inverted.doc += …” would raise and therefore must be guarded.

xaxis_inverted.__doc__ += textwrap.dedent("""

.. admonition:: Discouraged

The use of this method is discouraged.
Use `.Axes.get_xinverted` instead.
""")

def get_xbound(self):
"""
Expand Down Expand Up @@ -3856,15 +3872,30 @@ def invert_yaxis(self):
"""
Invert the y-axis.

.. admonition:: Discouraged

The use of this method is discouraged.
Use `.Axes.set_yinverted` instead.

See Also
--------
yaxis_inverted
get_yinverted
get_ylim, set_ylim
get_ybound, set_ybound
"""
self.yaxis.set_inverted(not self.yaxis.get_inverted())

set_yinverted = _axis_method_wrapper("yaxis", "set_inverted")
get_yinverted = _axis_method_wrapper("yaxis", "get_inverted")
yaxis_inverted = _axis_method_wrapper("yaxis", "get_inverted")
if yaxis_inverted.__doc__:
yaxis_inverted.__doc__ += textwrap.dedent("""

.. admonition:: Discouraged

The use of this method is discouraged.
Use `.Axes.get_yinverted` instead.
""")

def get_ybound(self):
"""
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/axes/_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ class _AxesBase(martist.Artist):
def get_autoscaley_on(self) -> bool: ...
def set_autoscalex_on(self, b: bool) -> None: ...
def set_autoscaley_on(self, b: bool) -> None: ...
def get_xinverted(self) -> bool: ...
def xaxis_inverted(self) -> bool: ...
def set_xinverted(self, inverted: bool) -> None: ...
def get_xscale(self) -> str: ...
def set_xscale(self, value: str | ScaleBase, **kwargs) -> None: ...
def get_xticks(self, *, minor: bool = ...) -> np.ndarray: ...
Expand All @@ -430,7 +432,9 @@ class _AxesBase(martist.Artist):
fontdict: dict[str, Any] | None = ...,
**kwargs
) -> list[Text]: ...
def get_yinverted(self) -> bool: ...
def yaxis_inverted(self) -> bool: ...
def set_yinverted(self, inverted: bool) -> None: ...
def get_yscale(self) -> str: ...
def set_yscale(self, value: str | ScaleBase, **kwargs) -> None: ...
def get_yticks(self, *, minor: bool = ...) -> np.ndarray: ...
Expand Down
17 changes: 16 additions & 1 deletion lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1888,16 +1888,31 @@ def invert_zaxis(self):
"""
Invert the z-axis.

.. admonition:: Discouraged

The use of this method is discouraged.
Use `.Axes3D.set_zinverted` instead.

See Also
--------
zaxis_inverted
get_zinverted
get_zlim, set_zlim
get_zbound, set_zbound
"""
bottom, top = self.get_zlim()
self.set_zlim(top, bottom, auto=None)

set_zinverted = _axis_method_wrapper("zaxis", "set_inverted")
get_zinverted = _axis_method_wrapper("zaxis", "get_inverted")
zaxis_inverted = _axis_method_wrapper("zaxis", "get_inverted")
if zaxis_inverted.__doc__:
zaxis_inverted.__doc__ += textwrap.dedent("""

.. admonition:: Discouraged

The use of this method is discouraged.
Use `.Axes3D.get_zinverted` instead.
""")

def get_zbound(self):
"""
Expand Down
Loading
0