Skip to content

Commit 87dc956

Browse files
committed
Merge branch 'clabel_format'
2 parents bbfae9b + 9114598 commit 87dc956

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2011-02-20 clabel accepts a callable as an fmt kwarg; modified
2+
patch by Daniel Hyams. - EF
3+
14
2011-02-18 scatter([], []) is now valid. Also fixed issues
25
with empty collections - BVR
36

examples/pylab_examples/contour_label_demo.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
import matplotlib.cm as cm
1111
import matplotlib.mlab as mlab
12+
import matplotlib.ticker as ticker
1213
import matplotlib.pyplot as plt
1314

1415
matplotlib.rcParams['xtick.direction'] = 'out'
@@ -68,7 +69,15 @@ def __repr__(self):
6869
# Label every other level using strings
6970
plt.clabel(CS,CS.levels[::2],inline=True,fmt=fmt,fontsize=10)
7071

71-
##################################################
72-
# Show the hole thing
73-
##################################################
72+
# Use a Formatter
73+
74+
plt.figure()
75+
76+
CS = plt.contour(X, Y, 100**Z, locator=plt.LogLocator())
77+
fmt = ticker.LogFormatterMathtext()
78+
fmt.create_dummy_axis()
79+
plt.clabel(CS, CS.levels, fmt=fmt)
80+
plt.title("$100^Z$")
81+
7482
plt.show()
83+

lib/matplotlib/contour.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def clabel(self, *args, **kwargs):
9595
a format string for the label. Default is '%1.3f'
9696
Alternatively, this can be a dictionary matching contour
9797
levels with arbitrary strings to use for each contour level
98-
(i.e., fmt[level]=string)
98+
(i.e., fmt[level]=string), or it can be any callable, such
99+
as a :class:`~matplotlib.ticker.Formatter` instance, that
100+
returns a string when called with a numeric contour level.
99101
100102
*manual*:
101103
if *True*, contour labels will be placed manually using
@@ -326,6 +328,8 @@ def get_text(self, lev, fmt):
326328
else:
327329
if isinstance(fmt,dict):
328330
return fmt[lev]
331+
elif callable(fmt):
332+
return fmt(lev)
329333
else:
330334
return fmt%lev
331335

0 commit comments

Comments
 (0)