@@ -49,7 +49,7 @@ class ContourLabeler:
49
49
"""Mixin to provide labelling capability to `.ContourSet`."""
50
50
51
51
def clabel (self , levels = None , * ,
52
- fontsize = None , inline = True , inline_spacing = 5 , fmt = '%1.3f' ,
52
+ fontsize = None , inline = True , inline_spacing = 5 , fmt = None ,
53
53
colors = None , use_clabeltext = False , manual = False ,
54
54
rightside_up = True , zorder = None ):
55
55
"""
@@ -90,14 +90,17 @@ def clabel(self, levels=None, *,
90
90
This spacing will be exact for labels at locations where the
91
91
contour is straight, less so for labels on curved contours.
92
92
93
- fmt : str or dict, default: '%1.3f'
94
- A format string for the label.
93
+ fmt : `.Formatter` or str or callable or dict, optional
94
+ How the levels are formatted:
95
95
96
- Alternatively, this can be a dictionary matching contour levels
97
- with arbitrary strings to use for each contour level (i.e.,
98
- fmt[level]=string), or it can be any callable, such as a
99
- `.Formatter` instance, that returns a string when called with a
100
- numeric contour level.
96
+ - If a `.Formatter`, it is used to format all levels at once, using
97
+ its `.Formatter.format_ticks` method.
98
+ - If a str, it is interpreted as a %-style format string.
99
+ - If a callable, it is called with one level at a time and should
100
+ return the corresponding label.
101
+ - If a dict, it should directly map levels to labels.
102
+
103
+ The default is to use a standard `.ScalarFormatter`.
101
104
102
105
manual : bool or iterable, default: False
103
106
If ``True``, contour labels will be placed manually using
@@ -142,6 +145,9 @@ def clabel(self, levels=None, *,
142
145
# labels method (case of automatic label placement) or
143
146
# `BlockingContourLabeler` (case of manual label placement).
144
147
148
+ if fmt is None :
149
+ fmt = ticker .ScalarFormatter (useOffset = False )
150
+ fmt .create_dummy_axis ()
145
151
self .labelFmt = fmt
146
152
self ._use_clabeltext = use_clabeltext
147
153
# Detect if manual selection is desired and remove from argument list.
@@ -259,13 +265,14 @@ def get_text(self, lev, fmt):
259
265
"""Get the text of the label."""
260
266
if isinstance (lev , str ):
261
267
return lev
268
+ elif isinstance (fmt , dict ):
269
+ return fmt .get (lev , '%1.3f' )
270
+ elif callable (getattr (fmt , "format_ticks" , None )):
271
+ return fmt .format_ticks ([* self .labelLevelList , lev ])[- 1 ]
272
+ elif callable (fmt ):
273
+ return fmt (lev )
262
274
else :
263
- if isinstance (fmt , dict ):
264
- return fmt .get (lev , '%1.3f' )
265
- elif callable (fmt ):
266
- return fmt (lev )
267
- else :
268
- return fmt % lev
275
+ return fmt % lev
269
276
270
277
def locate_label (self , linecontour , labelwidth ):
271
278
"""
0 commit comments