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

Commit65d7ca0

Browse files
committed
Compactify the implementation of ContourLabeler.add_label_near.
The old implementation was quite expansive; compactify it a bit. Alsoreformat the docs.
1 parentda66560 commit65d7ca0

File tree

1 file changed

+16
-32
lines changed

1 file changed

+16
-32
lines changed

‎lib/matplotlib/contour.py

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -455,64 +455,47 @@ def add_label_clabeltext(self, x, y, rotation, lev, cvalue):
455455
defadd_label_near(self,x,y,inline=True,inline_spacing=5,
456456
transform=None):
457457
"""
458-
Add a label near the point (x, y). If transform is None
459-
(default), (x, y) is in data coordinates; if transform is
460-
False, (x, y) is in display coordinates; otherwise, the
461-
specified transform will be used to translate (x, y) into
462-
display coordinates.
458+
Add a label near the point ``(x, y)``.
463459
464460
Parameters
465461
----------
466462
x, y : float
467463
The approximate location of the label.
468-
469464
inline : bool, default: True
470465
If *True* remove the segment of the contour beneath the label.
471-
472466
inline_spacing : int, default: 5
473467
Space in pixels to leave on each side of label when placing
474468
inline. This spacing will be exact for labels at locations where
475469
the contour is straight, less so for labels on curved contours.
470+
transform : `.Transform` or `False`, default: ``self.axes.transData``
471+
A transform applied to ``(x, y)`` before labeling. The default
472+
causes ``(x, y)`` to be interpreted as data coordinates. `False`
473+
is a synonym for `.IdentityTransform`; i.e. ``(x, y)`` should be
474+
interpreted as display coordinates.
476475
"""
477476

478477
iftransformisNone:
479478
transform=self.axes.transData
480-
481479
iftransform:
482480
x,y=transform.transform((x,y))
483481

484482
# find the nearest contour _in screen units_
485483
conmin,segmin,imin,xmin,ymin=self.find_nearest_contour(
486484
x,y,self.labelIndiceList)[:5]
487485

488-
#Thecalc_label_rot_and_inline routine requires that (xmin, ymin)
486+
# calc_label_rot_and_inline requires that (xmin, ymin)
489487
# be a vertex in the path. So, if it isn't, add a vertex here
490-
491-
# grab the paths from the collections
492-
paths=self.collections[conmin].get_paths()
493-
# grab the correct segment
494-
active_path=paths[segmin]
495-
# grab its vertices
496-
lc=active_path.vertices
497-
# sort out where the new vertex should be added data-units
488+
paths=self.collections[conmin].get_paths()# paths of correct coll.
489+
lc=paths[segmin].vertices# vertices of correct segment
490+
# Where should the new vertex be added in data-units?
498491
xcmin=self.axes.transData.inverted().transform([xmin,ymin])
499-
# if there isn't a vertex close enough
500-
ifnotnp.allclose(xcmin,lc[imin]):
501-
# insert new data into the vertex list
502-
lc=np.row_stack([lc[:imin],xcmin,lc[imin:]])
503-
# replace the path with the new one
504-
paths[segmin]=mpath.Path(lc)
492+
ifnotnp.allclose(xcmin,lc[imin]):# If no vertex is close enough...
493+
lc=np.insert(lc,imin,xcmin,axis=0)# add new pt in vertices,
494+
paths[segmin]=mpath.Path(lc)# and replace path by new one.
505495

506496
# Get index of nearest level in subset of levels used for labeling
507497
lmin=self.labelIndiceList.index(conmin)
508498

509-
# Coordinates of contour
510-
paths=self.collections[conmin].get_paths()
511-
lc=paths[segmin].vertices
512-
513-
# In pixel/screen space
514-
slc=self.axes.transData.transform(lc)
515-
516499
# Get label width for rotating labels and breaking contours
517500
lw=self.get_label_width(self.labelLevelList[lmin],
518501
self.labelFmt,self.labelFontSizeList[lmin])
@@ -522,7 +505,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,
522505

523506
# Figure out label rotation.
524507
rotation,nlc=self.calc_label_rot_and_inline(
525-
slc,imin,lw,lcifinlineelseNone,inline_spacing)
508+
self.axes.transData.transform(lc),# to pixel space.
509+
imin,lw,lcifinlineelseNone,inline_spacing)
526510

527511
self.add_label(xmin,ymin,rotation,self.labelLevelList[lmin],
528512
self.labelCValueList[lmin])
@@ -1316,7 +1300,7 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
13161300
# Nonetheless, improvements could probably be made.
13171301

13181302
ifindicesisNone:
1319-
indices=list(range(len(self.levels)))
1303+
indices=range(len(self.levels))
13201304

13211305
d2min=np.inf
13221306
conmin=None

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp