Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8874cb0

Browse files
committed
DOC: document fmt_xdata and fmt_ydata
Closes#9593
1 parenta844eca commit8874cb0

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
@@ -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 show 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={formtated_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'({abs(v):.3g})'
255+
256+
will format negative y-values with parenthesis rather than a negative sign. If
257+
these attributes are set to `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."""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp