Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit39d6d98

Browse files
committed
Inline and optimize ContourLabeler.get_label_coords.
`get_label_coords` is clearly an internal helper to `locate_label`; e.g.the `distances` parameter needs to be filled in with a very specificarray that's internally computed by `locate_label`. Inline it, whichalso makes clearer the possibility to save some further computation for`locate_label` to return a linearized index (the part that was commented"There must be a more efficient way..."; indeed converting the array toa list of tuples to get back the index seems pretty inefficient).Also better document the implementation of `get_label_coords` (I'm notactually changing any of the code logic).
1 parentf50fe72 commit39d6d98

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``ContourLabeler.get_label_coords`` is deprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
It is considered an internal helper.

‎lib/matplotlib/contour.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def too_close(self, x, y, lw):
217217
returnany((x-loc[0])**2+ (y-loc[1])**2<thresh
218218
forlocinself.labelXYs)
219219

220+
@_api.deprecated("3.4")
220221
defget_label_coords(self,distances,XX,YY,ysize,lw):
221222
"""
222223
Return x, y, and the index of a label location.
@@ -278,21 +279,14 @@ def locate_label(self, linecontour, labelwidth):
278279
"""
279280
Find good place to draw a label (relatively flat part of the contour).
280281
"""
281-
282-
# Number of contour points
283-
nsize=len(linecontour)
284-
iflabelwidth>1:
285-
xsize=int(np.ceil(nsize/labelwidth))
286-
else:
287-
xsize=1
288-
ifxsize==1:
289-
ysize=nsize
290-
else:
291-
ysize=int(labelwidth)
292-
293-
XX=np.resize(linecontour[:,0], (xsize,ysize))
294-
YY=np.resize(linecontour[:,1], (xsize,ysize))
295-
# I might have fouled up the following:
282+
ctr_size=len(linecontour)
283+
n_blocks=int(np.ceil(ctr_size/labelwidth))iflabelwidth>1else1
284+
block_size=ctr_sizeifn_blocks==1elseint(labelwidth)
285+
# Split contour into blocks of length ``block_size``, filling the last
286+
# block by cycling the contour start (per `np.resize` semantics). (Due
287+
# to cycling, the index returned is taken modulo ctr_size.)
288+
XX=np.resize(linecontour[:,0], (n_blocks,block_size))
289+
YY=np.resize(linecontour[:,1], (n_blocks,block_size))
296290
yfirst=YY[:, :1]
297291
ylast=YY[:,-1:]
298292
xfirst=XX[:, :1]
@@ -301,14 +295,21 @@ def locate_label(self, linecontour, labelwidth):
301295
L=np.hypot(xlast-xfirst,ylast-yfirst)
302296
# Ignore warning that divide by zero throws, as this is a valid option
303297
withnp.errstate(divide='ignore',invalid='ignore'):
304-
dist=np.sum(np.abs(s)/L,axis=-1)
305-
x,y,ind=self.get_label_coords(dist,XX,YY,ysize,labelwidth)
306-
307-
# There must be a more efficient way...
308-
lc= [tuple(l)forlinlinecontour]
309-
dind=lc.index((x,y))
310-
311-
returnx,y,dind
298+
distances=np.sum(np.abs(s)/L,axis=-1)
299+
# Labels are drawn in the middle of the block (``hbsize``) where the
300+
# contour is the closest (per ``distances``) to a straight line, but
301+
# not `too_close()` to a preexisting label.
302+
hbsize=block_size//2
303+
adist=np.argsort(distances)
304+
foridxinadist:
305+
x,y=XX[idx,hbsize],YY[idx,hbsize]
306+
ifself.too_close(x,y,labelwidth):
307+
continue
308+
returnx,y, (idx*block_size+hbsize)%ctr_size
309+
# If all candidates are `too_close()`, just use the straightest part.
310+
idx=adist[0]
311+
x,y=XX[idx,hbsize],YY[idx,hbsize]
312+
returnx,y,hbsize%ctr_size
312313

313314
defcalc_label_rot_and_inline(self,slc,ind,lw,lc=None,spacing=5):
314315
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp