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

Skip to content

Commit 30d8d3b

Browse files
committed
DOC: document fmt_xdata, fmt_ydata, and fmt_ydata
Closes #9593
1 parent a844eca commit 30d8d3b

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

doc/api/axes_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ Interactive
523523
Axes.format_cursor_data
524524
Axes.format_xdata
525525
Axes.format_ydata
526+
Axes.fmt_xdata
527+
Axes.fmt_ydata
526528

527529
Axes.mouseover
528530
Axes.in_axes

galleries/users_explain/figure/interactive.rst

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

237237

238+
Position Format
239+
---------------
240+
241+
The location of the cursor is shown in the UI and generated via the
242+
`~axes.Axes.format_coord` method which in turn calls the
243+
`~axes.Axes.format_xdata` and `~axes.Axes.format_ydata` methods. The hard
244+
coded format in `~axes.Axes.format_coord` is ``f'x={formatted_x}
245+
y={formatted_y}'``.
246+
247+
To easily customize how the x and y values are formatted, you can set the
248+
`.axes.Axes.fmt_xdata` and `.axes.Axes.fmt_ydata` attributes on the
249+
`~axes.Axes` instance. The values are expected to be functions that
250+
take a float and return a string. For example ::
251+
252+
fig, ax = plt.subplots()
253+
ax.set_ylim(-5, 5)
254+
ax.fmt_ydata = lambda v: f'{v:.3g}' if v > 0 else f'({-v:.3g})'
255+
256+
will format negative y-values with parenthesis rather than a negative sign. If
257+
these attributes are set to 8000 `None`, then the `.Formatter.format_data_short`
258+
method on the major formatter of the respective axes will be used instead.
259+
260+
238261
.. _other-shells:
239262

240263
Other Python prompts

lib/matplotlib/axes/_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,20 @@ class _AxesBase(martist.Artist):
561561

562562
_subclass_uses_cla = False
563563

564+
#: Callable to format the x-data in an interactive window.
565+
#:
566+
#: The expected signature is ::
567+
#:
568+
#: def fmt(val: float, /) -> str: ...
569+
fmt_xdata = None
570+
571+
#: Callable to format the y-data in an interactive window
572+
#:
573+
#: The expected signature is ::
574+
#:
575+
#: def fmt(val: float, /) -> str: ...
576+
fmt_ydata = None
577+
564578
@property
565579
def _axis_map(self):
566580
"""A mapping of axis names, e.g. 'x', to `Axis` instances."""

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ class Axes3D(Axes):
6262
sx = _api.deprecate_privatize_attribute("3.7")
6363
sy = _api.deprecate_privatize_attribute("3.7")
6464

65+
#: Callable to format the z-data in an interactive window
66+
#:
67+
#: The expected signature is ::
68+
#:
69+
#: def fmt(val: float, /) -> str: ...
70+
fmt_zdata = None
71+
6572
def __init__(
6673
self, fig, rect=None, *args,
6774
elev=30, azim=-60, roll=0, sharez=None, proj_type='persp',

0 commit comments

Comments
 (0)
0