8000 DOC: document fmt_xdata, fmt_ydata, and fmt_ydata · matplotlib/matplotlib@91f986a · GitHub
[go: up one dir, main page]

Skip to content

Commit 91f986a

Browse files
committed
DOC: document fmt_xdata, fmt_ydata, and fmt_ydata
Closes #9593
1 parent 5bd9b9d commit 91f986a

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

doc/api/axes_api.rst

Lines changed: 2 additions & 0 deletions
< 8000 /div>
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ Interactive
549549
Axes.format_cursor_data
550550
Axes.format_xdata
551551
Axes.format_ydata
552+
Axes.fmt_xdata
553+
Axes.fmt_ydata
552554

553555
Axes.mouseover
554556
Axes.in_axes

galleries/users_explain/figure/interactive.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,29 @@ Preserve aspect ratio hold **CONTROL** when panning/zooming with mo
312312
================================== ===============================
313313

314314

315+
Position Format
316+
---------------
317+
318+
The location of the cursor is shown in the UI and generated via the
319+
`~axes.Axes.format_coord` method which in turn calls the
320+
`~axes.Axes.format_xdata` and `~axes.Axes.format_ydata` methods. The hard
321+
coded format in `~axes.Axes.format_coord` is ``f'x={formatted_x}
322+
y={formatted_y}'``.
323+
324+
To easily customize how the x and y values are formatted, you can set the
325+
`.axes.Axes.fmt_xdata` and `.axes.Axes.fmt_ydata` attributes on the
326+
`~axes.Axes` instance. The values are expected to be functions that
327+
take a float and return a string. For example ::
328+
329+
fig, ax = plt.subplots()
330+
ax.set_ylim(-5, 5)
331+
ax.fmt_ydata = lambda v: f'{v:.3g}' if v > 0 else f'({-v:.3g})'
332+
333+
will format negative y-values with parenthesis rather than a negative sign. If
334+
these attributes are set to `None`, then the `.Formatter.format_data_short`
335+
method on the major formatter of the respective axes will be used instead.
336+
337+
315338
.. _other-shells:
316339

317340
Other Python prompts

lib/matplotlib/axes/_base.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,26 @@ class _AxesBase(martist.Artist):
594594
- :doc:`Axis API </api/axis_api>`
595595
"""
596596

597+
#: Callable to format the x-data in an interactive window.
598+
#:
599+
#: The expected signature is ::
600+
#:
601+
#: def fmt(val: float, /) -> str: ...
602+
fmt_xdata = None
603+
604+
#: Callable to format the y-data in an interactive window
605+
#:
606+
#: The expected signature is ::
607+
#:
608+
#: def fmt(val: float, /) -> str: ...
609+
fmt_ydata = None
610+
611+
@property
612+
def _axis_map(self):
613+
"""A mapping of axis names, e.g. 'x', to `Axis` instances."""
614+
return {name: getattr(self, f"{name}axis")
615+
for name in self._axis_names}
616+
597617
def __str__(self):
598618
return "{0}({1[0]:g},{1[1]:g};{1[2]:g}x{1[3]:g})".format(
599619
type(self).__name__, self._position.bounds)

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class Axes3D(Axes):
5757
Axes._shared_axes["z"] = cbook.Grouper()
5858
Axes._shared_axes["view"] = cbook.Grouper()
5959

60+
#: Callable to format the z-data in an interactive window
61+
#:
62+
#: The expected signature is ::
63+
#:
64+
#: def fmt(val: float, /) -> str: ...
65+
fmt_zdata = None
66+
6067
def __init__(
6168
self, fig, rect=None, *args,
6269
elev=30, azim=-60, roll=0, shareview=None, sharez=None,

0 commit comments

Comments
 (0)
0