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

Skip to content

Commit 0b7d532

Browse files
committed
DOC: document fmt_xdata and fmt_ydata
Closes #9593
1 parent f3938be commit 0b7d532

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/api/axes_api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ Interactive
522522
Axes.format_cursor_data
523523
Axes.format_xdata
524524
Axes.format_ydata
525+
Axes.fmt_xdata
526+
Axes.fmt_ydata
525527

526528
Axes.mouseover
527529
Axes.in_axes

doc/users/explain/interactive.rst

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

229229

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

232255
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."""

0 commit comments

Comments
 (0)
0