8000 Default to ScalarFormatter for contour plots. · matplotlib/matplotlib@421daf3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 421daf3

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

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-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 countouring call to restore the old default label
5+
format.

lib/matplotlib/contour.py

Lines changed: 20 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,16 @@ 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. 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.
101103
102104
manual : bool or iterable, default: False
103105
If ``True``, contour labels will be placed manually using
@@ -142,6 +144,9 @@ def clabel(self, levels=None, *,
142144
# labels method (case of automatic label placement) or
143145
# `BlockingContourLabeler` (case of manual label placement).
144146

147+
if fmt is None:
148+
fmt = ticker.ScalarFormatter(useOffset=False)
149+
fmt.create_dummy_axis()
145150
self.labelFmt = fmt
146151
self._use_clabeltext = use_clabeltext
147152
# Detect if manual selection is desired and remove from argument list.
@@ -259,13 +264,14 @@ def get_text(self, lev, fmt):
259264
"""Get the text of the label."""
260265
if isinstance(lev, str):
261266
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)
262273
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
269275

270276
def locate_label(self, linecontour, labelwidth):
271277
"""

0 commit comments

Comments
 (0)
0