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

Skip to content
Open
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 doc/api/axes_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ Interactive
Axes.format_cursor_data
Axes.format_xdata
Axes.format_ydata
Axes.fmt_xdata
Axes.fmt_ydata

Axes.mouseover
Axes.in_axes
Expand Down
2 changes: 2 additions & 0 deletions doc/api/toolkits/mplot3d/axes3d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ Interactive
format_zdata
format_coord

fmt_zdata


Projection and perspective
--------------------------
Expand Down
23 changes: 23 additions & 0 deletions galleries/users_explain/figure/interactive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,29 @@ Preserve aspect ratio hold **CONTROL** when panning/zooming with mo
================================== ===============================


Position Format
---------------

The location of the cursor is shown in the UI and generated via the
`~axes.Axes.format_coord` method which in turn calls the
`~axes.Axes.format_xdata` and `~axes.Axes.format_ydata` methods. The hard
coded format in `~axes.Axes.format_coord` is ``f'x={formatted_x}
y={formatted_y}'``.

To easily customize how the x and y values are formatted, you can set the
`.axes.Axes.fmt_xdata` and `.axes.Axes.fmt_ydata` attributes on the
`~axes.Axes` instance. The values are expected to be functions that
take a float and return a string. For example ::

fig, ax = plt.subplots()
ax.set_ylim(-5, 5)
ax.fmt_ydata = lambda v: f'{v:.3g}' if v > 0 else f'({-v:.3g})'

will format negative y-values with parenthesis rather than a negative sign. If
these attributes are set to `None`, then the `.Formatter.format_data_short`
method on the major formatter of the respective axes will be used instead.


.. _other-shells:

Other Python prompts
Expand Down
19 changes: 18 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import Iterable, Sequence
from collections.abc import Iterable, Sequence, Callable
from contextlib import ExitStack
import functools
import inspect
Expand Down Expand Up @@ -594,6 +594,23 @@ class _AxesBase(martist.Artist):
- :doc:`Axis API </api/axis_api>`
"""

fmt_xdata: Callable[[float], str] | None
"""
Callable to format the x-data in an interactive window.

The expected signature is ::

def fmt(val: float, /) -> str: ...
"""

fmt_ydata: Callable[[float], str] | None
"""
Callable to format the y-data in an interactive window.

The expected signature is ::

def fmt(val: float, /) -> str: ...
"""
def __str__(self):
return "{0}({1[0]:g},{1[1]:g};{1[2]:g}x{1[3]:g})".format(
type(self).__name__, self._position.bounds)
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 @@ -11,6 +11,7 @@
"""

from collections import defaultdict
from collections.abc import Callable
import itertools
import math
import textwrap
Expand Down Expand Up @@ -57,6 +58,15 @@ class Axes3D(Axes):
Axes._shared_axes["z"] = cbook.Grouper()
Axes._shared_axes["view"] = cbook.Grouper()

fmt_zdata: Callable[[float], str] | None
Copy link
Member

Choose a reason for hiding this comment

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

Setting this to something seems to fix it, though again, not sure why it doesn't seem necessary on the others.

Suggested change
fmt_zdata: Callable[[float], str] | None
fmt_zdata: Callable[[float], str] | None = None

Copy link
Member

@QuLogic QuLogic Sep 12, 2025

Choose a reason for hiding this comment

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

I just had an idea... Neither of these exist as attributes on the class:

>>> from matplotlib.axes import Axes
>>> Axes.fmt_xdata
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    Axes.fmt_xdata
AttributeError: type object 'Axes' has no attribute 'fmt_xdata'. Did you mean: 'format_xdata'?

>>> from mpl_toolkits.mplot3d.axes3d import Axes3D
>>> Axes3D.fmt_zdata
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    Axes3D.fmt_zdata
AttributeError: type object 'Axes3D' has no attribute 'fmt_zdata'. Did you mean: 'format_zdata'?

and yet the former works.

But the difference between 2D and 3D Axes is the former has type stubs and the latter doesn't. Unfortunately, removing fmt_xdata/fmt_ydata from the _base.pyi type stub did not break the docs. So I still don't know why it doesn't work.

"""
Callable to format the z-data in an interactive window.

The expected signature is ::

def fmt(val: float, /) -> str: ...
"""

def __init__(
self, fig, rect=None, *args,
elev=30, azim=-60, roll=0, shareview=None, sharez=None,
Expand Down
Loading