8000 DOC: document fmt_xdata, fmt_ydata, and fmt_zdata by tacaswell · Pull Request #25187 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

DOC: document fmt_xdata, fmt_ydata, and fmt_zdata #25187

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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 @@ -549,6 +549,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
18 changes: 18 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from operator import attrgetter
import re
import textwrap
from typing import Callable
import types

import numpy as np
Expand Down Expand Up @@ -594,6 +595,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 @@ -14,6 +14,7 @@
import itertools
import math
import textwrap
from typing import Callable
import warnings

import numpy as np
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
"""
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
0