@@ -38,38 +38,28 @@ class ClabelText(text.Text):
38
38
angle in the pixel coordinate assuming that the input rotation is
39
39
an angle in data coordinate (or whatever transform set).
40
40
"""
41
+
41
42
def get_rotation (self ):
42
- angle = text .Text .get_rotation (self )
43
- trans = self .get_transform ()
44
- x , y = self .get_position ()
45
- new_angles = trans .transform_angles (np .array ([angle ]),
46
- np .array ([[x , y ]]))
47
- return new_angles [0 ]
43
+ new_angle , = self .get_transform ().transform_angles (
44
+ [text .Text .get_rotation (self )], [self .get_position ()])
45
+ return new_angle
48
46
49
47
50
48
class ContourLabeler (object ):
51
49
"""Mixin to provide labelling capability to `.ContourSet`."""
52
50
53
- def clabel (self , * args ,
51
+ def clabel (self , levels = None , * ,
54
52
fontsize = None , inline = True , inline_spacing = 5 , fmt = '%1.3f' ,
55
53
colors = None , use_clabeltext = False , manual = False ,
56
54
rightside_up = True ):
57
55
"""
58
56
Label a contour plot.
59
57
60
- Call signature::
61
-
62
- clabel(cs, [levels,] **kwargs)
63
-
64
- Adds labels to line contours in *cs*, where *cs* is a
65
- :class:`~matplotlib.contour.ContourSet` object returned by
66
- ``contour()``.
58
+ Adds labels to line contours in this `.ContourSet` (which inherits from
59
+ this mixin class).
67
60
68
61
Parameters
69
62
----------
70
- cs : `.ContourSet`
71
- The ContourSet to label.
72
-
73
63
levels : array-like, optional
74
64
A list of level values, that should be labeled. The list must be
75
65
a subset of ``cs.levels``. If not given, all levels are labeled.
@@ -105,11 +95,11 @@ def clabel(self, *args,
105
95
fmt : string or dict, optional
106
96
A format string for the label. Default is '%1.3f'
107
97
108
- Alternatively, this can be a dictionary matching contour
109
- levels with arbitrary strings to use for each contour level
110
- (i.e., fmt[level]=string), or it can be any callable, such
111
- as a :class:`~matplotlib.ticker. Formatter` instance, that
112
- returns a string when called with a numeric contour level.
98
+ Alternatively, this can be a dictionary matching contour levels
99
+ with arbitrary strings to use for each contour level (i.e.,
100
+ fmt[level]=string), or it can be any callable, such as a
101
+ `. Formatter` instance, that returns a string when called with a
102
+ numeric contour level.
113
103
114
104
manual : bool or iterable, optional
115
105
If ``True``, contour labels will be placed manually using
@@ -161,21 +151,19 @@ def clabel(self, *args,
161
151
self .labelManual = manual
162
152
self .rightside_up = rightside_up
163
153
164
- if len ( args ) == 0 :
154
+ if levels is None :
165
155
levels = self .levels
166
156
indices = list (range (len (self .cvalues )))
167
- elif len ( args ) == 1 :
168
- levlabs = list (args [ 0 ] )
157
+ else :
158
+ levlabs = list (levels )
169
159
indices , levels = [], []
170
160
for i , lev in enumerate (self .levels ):
171
161
if lev in levlabs :
172
162
indices .append (i )
173
163
levels .append (lev )
174
164
if len (levels ) < len (levlabs ):
175
- raise ValueError ("Specified levels {} don't match available "
176
- "levels {}" .format (levlabs , self .levels ))
177
- else :
178
- raise TypeError ("Illegal arguments to clabel, see help(clabel)" )
165
+ raise ValueError (f"Specified levels { levlabs } don't match "
166
+ f"available levels { self .levels } " )
179
167
self .labelLevelList = levels
180
168
self .labelIndiceList = indices
181
169
@@ -197,15 +185,12 @@ def clabel(self, *args,
197
185
198
186
if np .iterable (self .labelManual ):
199
187
for x , y in self .labelManual :
200
- self .add_label_near (x , y , inline ,
201
- inline_spacing )
202
-
188
+ self .add_label_near (x , y , inline , inline_spacing )
203
189
elif self .labelManual :
204
190
print ('Select label locations manually using first mouse button.' )
205
191
print ('End manual selection with second mouse button.' )
206
192
if not inline :
207
193
print ('Remove last label by clicking third mouse button.' )
208
-
209
194
blocking_contour_labeler = BlockingContourLabeler (self )
210
195
blocking_contour_labeler (inline , inline_spacing )
211
196
else :
@@ -262,13 +247,10 @@ def get_label_width(self, lev, fmt, fsize):
262
247
"""
263
248
if not isinstance (lev , str ):
264
249
lev = self .get_text (lev , fmt )
265
-
266
250
lev , ismath = text .Text ()._preprocess_math (lev )
267
251
if ismath == 'TeX' :
268
- if not hasattr (self , '_TeX_manager' ):
269
- self ._TeX_manager = texmanager .TexManager ()
270
- lw , _ , _ = self ._TeX_manager .get_text_width_height_descent (lev ,
271
- fsize )
252
+ lw , _ , _ = (texmanager .TexManager ()
253
+ .get_text_width_height_descent (lev , fsize ))
272
254
elif ismath :
273
255
if not hasattr (self , '_mathtext_parser' ):
274
256
self ._mathtext_parser = mathtext .MathTextParser ('bitmap' )
@@ -277,8 +259,7 @@ def get_label_width(self, lev, fmt, fsize):
277
259
lw = img .get_width () # at dpi=72, the units are PostScript points
278
260
else :
279
261
# width is much less than "font size"
280
- lw = (len (lev )) * fsize * 0.6
281
-
262
+ lw = len (lev ) * fsize * 0.6
282
263
return lw
283
264
284
265
def set_label_props (self , label , text , color ):
@@ -465,7 +446,6 @@ def add_label(self, x, y, rotation, lev, cvalue):
465
446
"""
466
447
Add contour label using :class:`~matplotlib.text.Text` class.
467
448
"""
468
-
469
449
t = self ._get_label_text (x , y , rotation )
470
450
self ._add_label (t , x , y , lev , cvalue )
471
451
@@ -477,7 +457,6 @@ def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
477
457
# the data coordinate and create a label using ClabelText
478
458
# class. This way, the rotation of the clabel is along the
479
459
# contour line always.
480
-
481
460
t = self ._get_label_clabeltext (x , y , rotation )
482
461
self ._add_label (t , x , y , lev , cvalue )
483
462
@@ -633,8 +612,7 @@ def labels(self, inline, inline_spacing):
633
612
# After looping over all segments on a contour, remove old
634
613
# paths and add new ones if inlining
635
614
if inline :
636
- del paths [:]
637
- paths .extend (additions )
615
+ paths [:] = additions
638
616
639
617
640
618
def _find_closest_point_on_leg (p1 , p2 , p0 ):
@@ -1032,15 +1010,11 @@ def legend_elements(self, variable_name='x', str_format=str):
1032
1010
upper = str_format (upper )
1033
1011
1034
1012
if i == 0 and self .extend in ('min' , 'both' ):
1035
- labels .append (r'$%s \leq %s$' % (variable_name ,
1036
- lower ))
1013
+ labels .append (fr'${ variable_name } \leq { lower } s$' )
1037
1014
elif i == n_levels - 1 and self .extend in ('max' , 'both' ):
1038
- labels .append (r'$%s > %s$' % (variable_name ,
1039
- upper ))
1015
+ labels .append (fr'${ variable_name } > { upper } s$' )
1040
1016
else :
1041
- labels .append (r'$%s < %s \leq %s$' % (lower ,
1042
- variable_name ,
1043
- upper ))
1017
+ labels .append (fr'${ lower } < { variable_name } \leq { upper } $' )
1044
1018
else :
1045
1019
for collection , level in zip (self .collections , self .levels ):
1046
1020
@@ -1050,7 +1024,7 @@ def legend_elements(self, variable_name='x', str_format=str):
1050
1024
artists .append (patch )
1051
1025
# format the level for insertion into the labels
1052
1026
level = str_format (level )
1053
- labels .append (r'$%s = %s$' % ( variable_name , level ) )
1027
+ labels .append (fr'$ { variable_name } = { level } $' )
1054
1028
1055
1029
return artists , labels
1056
1030
@@ -1063,7 +1037,7 @@ def _process_args(self, *args, **kwargs):
1063
1037
"""
1064
1038
self .levels = args [0 ]
1065
1039
self .allsegs = args [1 ]
1066
- self .allkinds = len (args ) > 2 and args [ 2 ] or None
1040
+ self .allkinds = args [ 2 ] if len (args ) > 2 else None
1067
1041
self .zmax = np .max (self .levels )
1068
1042
self .zmin = np .min (self .levels )
1069
1043
0 commit comments