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

Skip to content

Commit d651301

Browse files
committed
Default to ScalarFormatter for contour plots.
1 parent 8a44942 commit d651301

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Contour plots now default to using ScalarFormatter
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Pass ``fmt="%1.3f"`` to the contouring call to restore the old default label
5+
format.

lib/matplotlib/contour.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ContourLabeler:
4949
"""Mixin to provide labelling capability to `.ContourSet`."""
5050

5151
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,
5353
colors=None, use_clabeltext=False, manual=False,
5454
rightside_up=True, zorder=None):
5555
"""
@@ -90,14 +90,17 @@ def clabel(self, levels=None, *,
9090
This spacing will be exact for labels at locations where the
9191
contour is straight, less so for labels on curved contours.
9292
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:
9595
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`.
101104
102105
manual : bool or iterable, default: False
103106
If ``True``, contour labels will be placed manually using
@@ -142,6 +145,9 @@ def clabel(self, levels=None, *,
142145
# labels method (case of automatic label placement) or
143146
# `BlockingContourLabeler` (case of manual label placement).
144147

148+
if fmt is None:
149+
fmt = ticker.ScalarFormatter(useOffset=False)
150+
fmt.create_dummy_axis()
145151
self.labelFmt = fmt
146152
self._use_clabeltext = use_clabeltext
147153
# Detect if manual selection is desired and remove from argument list.
@@ -259,13 +265,14 @@ def get_text(self, lev, fmt):
259265
"""Get the text of the label."""
260266
if isinstance(lev, str):
261267
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)
262274
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
269276

270277
def locate_label(self, linecontour, labelwidth):
271278
"""

0 commit comments

Comments
 (0)