From 97f26505c47ae371a426812376ee747bd51056d6 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Wed, 12 Jul 2023 12:21:20 +0100 Subject: [PATCH 1/2] Use transformed paths for contour labelling decisions --- lib/matplotlib/contour.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 625c3524bfcb..ce39f5042c70 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -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) @@ -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 @@ -582,7 +582,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( @@ -614,8 +615,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 # 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) @@ -626,7 +628,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: From 07f694dc3f0ef90e95e3dce44d4f4857b5dc6e55 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Wed, 12 Jul 2023 12:57:34 +0100 Subject: [PATCH 2/2] sometimes label index is the only MOVETO --- lib/matplotlib/contour.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index ce39f5042c70..9bd9c66aa97e 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -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: