Skip to content

Use transformed paths for contour labelling decisions #26297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _split_path_and_get_label_rotation(self, path, idx, screen_pos, lw, spacing=
Parameters
----------
path : Path
The path where the label will be inserted, in data space.
The path where the label will be inserted, in screen space.
idx : int
The vertex index after which the label will be inserted.
screen_pos : (float, float)
Expand Down Expand Up @@ -352,7 +352,7 @@ def _split_path_and_get_label_rotation(self, path, idx, screen_pos, lw, spacing=
if hasattr(self, "_old_style_split_collections"):
del self._old_style_split_collections # Invalidate them.

xys = path.vertices
xys = self.get_transform().inverted().transform(path.vertices)
codes = path.codes

# Insert a vertex at idx/pos (converting back to data space), if there isn't yet
Expand All @@ -370,7 +370,10 @@ def _split_path_and_get_label_rotation(self, path, idx, screen_pos, lw, spacing=
# path always starts with a MOVETO, and we consider there's an implicit
# MOVETO (closing the last path) at the end.
movetos = (codes == Path.MOVETO).nonzero()[0]
start = movetos[movetos < idx][-1]
try:
start = movetos[movetos < idx][-1]
except IndexError:
start = idx
try:
stop = movetos[movetos > idx][0]
except IndexError:
Expand Down Expand Up @@ -582,7 +585,8 @@ def add_label_near(self, x, y, inline=True, inline_spacing=5,

idx_level_min, idx_vtx_min, proj = self._find_nearest_contour(
(x, y), self.labelIndiceList)
path = self._paths[idx_level_min]
path = self.get_transform().transform_path(self._paths[idx_level_min])

level = self.labelIndiceList.index(idx_level_min)
label_width = self._get_nth_label_width(level)
rotation, path = self._split_path_and_get_label_rotation(
Expand Down Expand Up @@ -614,8 +618,9 @@ def labels(self, inline, inline_spacing):
trans = self.get_transform()
label_width = self._get_nth_label_width(idx)
additions = []
for subpath in self._paths[icon]._iter_connected_components():
screen_xys = trans.transform(subpath.vertices)
for subpath in trans.transform_path(
self._paths[icon])._iter_connected_components():
screen_xys = subpath.vertices
Comment on lines +621 to +623
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could save the inverse transform below by leaving the for loop as it was, and then calculating tsubpath = trans.transform_path(subpath) inside the loop and then use subpath/tsubpath in the right locations below.

# Check if long enough for a label
if self.print_label(screen_xys, label_width):
x, y, idx = self.locate_label(screen_xys, label_width)
Expand All @@ -626,7 +631,7 @@ def labels(self, inline, inline_spacing):
if inline: # If inline, add new contours
additions.append(path)
else: # If not adding label, keep old path
additions.append(subpath)
additions.append(trans.inverted().transform_path(subpath))
# After looping over all segments on a contour, replace old path by new one
# if inlining.
if inline:
Expand Down