File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed
Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
232255Other Python prompts
Original file line number Diff line number Diff 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."""
You can’t perform that action at this time.
0 commit comments