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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
tacaswell wants to merge3 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromtacaswell:doc/fmt_xydata
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsdoc/api/axes_api.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsdoc/api/toolkits/mplot3d/axes3d.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsgalleries/users_explain/figure/interactive.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/matplotlib/axes/_base.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionslib/mpl_toolkits/mplot3d/axes3d.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp