Skip to content

Commit

Permalink
Add test for disconnected paths, remove invalidated paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ksunden committed Oct 20, 2023
1 parent f0a3a09 commit 5cf3da9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ def _split_path_and_get_label_rotation(self, path, idx, screen_pos, lw, spacing=
taken into account when breaking the path, but not when computing the angle.
"""
if hasattr(self, "_old_style_split_collections"):
vis = False
for coll in self._old_style_split_collections:
vis |= coll.get_visible()
coll.remove()
self.set_visible(vis)
del self._old_style_split_collections # Invalidate them.

xys = path.vertices
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,31 @@ def test_contour_manual_labels(split_collections):

plt.figure(figsize=(6, 2), dpi=200)
cs = plt.contour(x, y, z)

_maybe_split_collections(split_collections)

pts = np.array([(1.0, 3.0), (1.0, 4.4), (1.0, 6.0)])
plt.clabel(cs, manual=pts)
pts = np.array([(2.0, 3.0), (2.0, 4.4), (2.0, 6.0)])
plt.clabel(cs, manual=pts, fontsize='small', colors=('r', 'g'))


@pytest.mark.parametrize("split_collections", [False, True])
@image_comparison(['contour_disconnected_segments'],
remove_text=True, style='mpl20', extensions=['png'])
def test_contour_label_with_disconnected_segments(split_collections):
x, y = np.mgrid[-1:1:21j, -1:1:21j]
z = 1 / np.sqrt(0.01 + (x + 0.3) ** 2 + y ** 2)
z += 1 / np.sqrt(0.01 + (x - 0.3) ** 2 + y ** 2)

plt.figure()
cs = plt.contour(x, y, z, levels=[7])

# Adding labels should invalidate the old style
_maybe_split_collections(split_collections)

cs.clabel(manual=[(0.2, 0.1)])

_maybe_split_collections(split_collections)


Expand Down Expand Up @@ -232,6 +252,9 @@ def test_labels(split_collections):
disp_units = [(216, 177), (359, 290), (521, 406)]
data_units = [(-2, .5), (0, -1.5), (2.8, 1)]

# Adding labels should invalidate the old style
_maybe_split_collections(split_collections)

CS.clabel()

for x, y in data_units:
Expand Down

0 comments on commit 5cf3da9

Please sign in to comment.