@@ -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,16 @@ 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. The default is to use a
98
+ standard `.ScalarFormatter`.
99
+ - If a str, it is interpreted as a %-style format string.
100
+ - If a callable, it is called with one level at a time and should
101
+ return the corresponding label.
102
+ - If a dict, it should directly map levels to labels.
101
103
102
104
manual : bool or iterable, default: False
103
105
If ``True``, contour labels will be placed manually using
@@ -142,6 +144,9 @@ def clabel(self, levels=None, *,
142
144
# labels method (case of automatic label placement) or
143
145
# `BlockingContourLabeler` (case of manual label placement).
144
146
147
+ if fmt is None :
148
+ fmt = ticker .ScalarFormatter (useOffset = False )
149
+ fmt .create_dummy_axis ()
145
150
self .labelFmt = fmt
146
151
self ._use_clabeltext = use_clabeltext
147
152
# Detect if manual selection is desired and remove from argument list.
@@ -259,13 +264,14 @@ def get_text(self, lev, fmt):
259
264
"""Get the text of the label."""
260
265
if isinstance (lev , str ):
261
266
return lev
267
+ elif isinstance (fmt , dict ):
268
+ return fmt .get (lev , '%1.3f' )
269
+ elif callable (getattr (fmt , "format_ticks" , None )):
270
+ return fmt .format_ticks ([* self .labelLevelList , lev ])[- 1 ]
271
+ elif callable (fmt ):
272
+ return fmt (lev )
262
273
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
274
+ return fmt % lev
269
275
270
276
def locate_label (self , linecontour , labelwidth ):
271
277
"""
0 commit comments