Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6871ecc

Browse files
authored
Merge pull request matplotlib#31414 from timhoffm/doc-formatter
DOC: Improve Formatter documentation
2 parents 4859b75 + 40bd49d commit 6871ecc

2 files changed

Lines changed: 54 additions & 10 deletions

File tree

doc/api/ticker_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.. automodule:: matplotlib.ticker
66
:members:
77
:undoc-members:
8+
:special-members: __call__
89
:show-inheritance:
910

1011

lib/matplotlib/ticker.py

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,41 +199,84 @@ def create_dummy_axis(self, **kwargs):
199199
class Formatter(TickHelper):
200200
"""
201201
Create a string based on a tick value and location.
202+
203+
The Formatter provides four formatting methods for different use cases:
204+
205+
- `format_ticks`: The public API for generating tick labels from a set of
206+
tick values.
207+
- `__call__`: The low-level primitive for formatting a single tick value,
208+
potentially in the context of multiple values.
209+
- `format_data`: Context-independent representation of a single value.
210+
Used internally, e.g. for offset and scientific-notation strings.
211+
- `format_data_short`: Concise plain-text representation of a single value
212+
for the interactive mouseover tooltip.
202213
"""
203214
# some classes want to see all the locs to help format
204215
# individual ones
205216
locs = []
206217

207218
def __call__(self, x, pos=None):
208219
"""
209-
Return the format for tick value *x* at index *pos*.
210-
``pos=None`` indicates an unspecified location.
220+
Return the tick label strings for value *x* at tick index *pos*.
221+
222+
This is the low-level formatting primitive for a single tick in
223+
the context of multiple ticks. Any context-dependent state
224+
(e.g. locs, offset, order of magnitude) must already be configured,
225+
typically by a prior call to ``format_ticks`` or ``set_locs``.
226+
227+
*pos* defines the index into ``self.locs`` so that the format can
228+
depend on the location. ``pos=None`` indicates an unspecified
229+
location.
230+
231+
The output may contain mathtext or LaTeX markup.
232+
233+
Subclasses must override this method.
211234
"""
212235
raise NotImplementedError('Derived must override')
213236

214237
def format_ticks(self, values):
215238
"""
216-
Return the tick labels strings for all *values*.
239+
Return the tick label strings for all *values*.
217240
218-
This method is the public API to generate a meaningful format for a set
219-
of values, e.g. by ensuring an appropriate precision.
241+
This is the public API for generating tick labels. It calls
242+
``set_locs`` to configure context-dependent formatting state before
243+
delegating to ``__call__`` for each individual value.
244+
245+
The output may contain mathtext or LaTeX markup.
246+
247+
Use this method (rather than ``__call__``) whenever formatting a
248+
complete set of tick values, so that formatters which need to see
249+
all tick locations (e.g. to determine precision, offsets, or which
250+
date components to display) can work correctly.
220251
"""
221252
self.set_locs(values)
222253
return [self(value, i) for i, value in enumerate(values)]
223254

224255
def format_data(self, value):
225256
"""
226-
Return the full string representation of the value with the
227-
position unspecified.
257+
Return the context-independent string representation of a single *value*.
258+
259+
This is used internally, e.g. for constructing offset and
260+
scientific-notation strings. It always formats with ``pos=None``
261+
and should return a context-independent representation
262+
rather than a concise tick label.
263+
264+
The output may contain mathtext or LaTeX markup.
228265
"""
229266
return self.__call__(value)
230267

231268
def format_data_short(self, value):
232269
"""
233-
Return the mouseover-text representation of a value.
270+
Return a short string representation of *value* for the mouseover
271+
tooltip (the coordinate display in the interactive figure window).
272+
273+
This should return concise, plain text (no mathtext / LaTeX).
274+
The precision is typically adapted to the current axis resolution
275+
so that neighbouring pixels produce distinguishable labels.
234276
235-
This defaults to the representation returned by `.Formatter.format_data`,
236-
but subclasses may override this.
277+
Defaults to `.Formatter.format_data`; subclasses should override
278+
this to provide a plain-text representation that is independent
279+
of the current tick locations.
237280
238281
Note: The mouseover text can be customized by setting the
239282
``Axes.fmt_xdata`` and ``Axes.fmt_ydata`` attributes.

0 commit comments

Comments
 (0)