Skip to content

Commit 20d7290

Browse files
committed
DOC: Improve Formatter documentation
Works towards matplotlib#31396.
1 parent 49fd492 commit 20d7290

1 file changed

Lines changed: 59 additions & 10 deletions

File tree

lib/matplotlib/ticker.py

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,41 +199,90 @@ 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`
206+
The public API for generating tick labels from a set of tick values.
207+
208+
`__call__`
209+
The low-level primitive for formatting a single tick value,
210+
potentially in the context of multiple values.
211+
212+
`format_data`
213+
Context-independent representation of a single value.
214+
Used internally, e.g. for offset and scientific-notation strings.
215+
216+
`format_data_short`
217+
Concise plain-text representation of a single value for the
218+
interactive mouseover tooltip.
202219
"""
203220
# some classes want to see all the locs to help format
204221
# individual ones
205222
locs = []
206223

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

214243
def format_ticks(self, values):
215244
"""
216-
Return the tick labels strings for all *values*.
245+
Return the tick label strings for all *values*.
246+
247+
This is the public API for generating tick labels. It calls
248+
`set_locs` to configure context-dependent formatting state before
249+
delegating to `__call__` for each individual value.
250+
251+
The output may contain mathtext or LaTeX markup.
217252
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.
253+
Use this method (rather than calling `__call__` directly) whenever
254+
formatting a complete set of tick values, so that formatters which
255+
need to see all tick locations (e.g. to determine precision, offsets,
256+
or which date components to display) can work correctly.
220257
"""
221258
self.set_locs(values)
222259
return [self(value, i) for i, value in enumerate(values)]
223260

224261
def format_data(self, value):
225262
"""
226-
Return the full string representation of the value with the
227-
position unspecified.
263+
Return the context-independent string representation of a single *value*.
264+
265+
This is used internally, e.g. for constructing offset and
266+
scientific-notation strings. It always formats with ``pos=None``
267+
and should return a context-independent representation
268+
rather than a concise tick label.
269+
270+
The output may contain mathtext or LaTeX markup.
228271
"""
229272
return self.__call__(value)
230273

231274
def format_data_short(self, value):
232275
"""
233-
Return the mouseover-text representation of a value.
276+
Return a short string representation of *value* for the mouseover
277+
tooltip (the coordinate display in the interactive figure window).
278+
279+
This should return concise, plain text (no mathtext / LaTeX).
280+
The precision is typically adapted to the current axis resolution
281+
so that neighbouring pixels produce distinguishable labels.
234282
235-
This defaults to the representation returned by `.Formatter.format_data`,
236-
but subclasses may override this.
283+
Defaults to `.Formatter.format_data`; subclasses should override
284+
this to provide a plain-text representation that is independent
285+
of the current tick locations.
237286
238287
Note: The mouseover text can be customized by setting the
239288
``Axes.fmt_xdata`` and ``Axes.fmt_ydata`` attributes.

0 commit comments

Comments
 (0)