Skip to content

Commit 432fa7d

Browse files
authored
Merge pull request matplotlib#26311 from rcomer/clabel-start
FIX: labels at start of contours
2 parents 5846838 + 7d13a70 commit 432fa7d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/matplotlib/contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def _split_path_and_get_label_rotation(self, path, idx, screen_pos, lw, spacing=
370370
# path always starts with a MOVETO, and we consider there's an implicit
371371
# MOVETO (closing the last path) at the end.
372372
movetos = (codes == Path.MOVETO).nonzero()[0]
373-
start = movetos[movetos < idx][-1]
373+
start = movetos[movetos <= idx][-1]
374374
try:
375375
stop = movetos[movetos > idx][0]
376376
except IndexError:

lib/matplotlib/tests/test_contour.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import platform
33
import re
4+
from unittest import mock
45

56
import contourpy # type: ignore
67
import numpy as np
@@ -234,6 +235,31 @@ def test_labels(split_collections):
234235
_maybe_split_collections(split_collections)
235236

236237

238+
def test_label_contour_start():
239+
# Set up data and figure/axes that result in automatic labelling adding the
240+
# label to the start of a contour
241+
242+
_, ax = plt.subplots(dpi=100)
243+
lats = lons = np.linspace(-np.pi / 2, np.pi / 2, 50)
244+
lons, lats = np.meshgrid(lons, lats)
245+
wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
246+
mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)
247+
data = wave + mean
248+
249+
cs = ax.contour(lons, lats, data)
250+
251+
with mock.patch.object(
252+
cs, '_split_path_and_get_label_rotation',
253+
wraps=cs._split_path_and_get_label_rotation) as mocked_splitter:
254+
# Smoke test that we can add the labels
255+
cs.clabel(fontsize=9)
256+
257+
# Verify at least one label was added to the start of a contour. I.e. the
258+
# splitting method was called with idx=0 at least once.
259+
idxs = [cargs[0][1] for cargs in mocked_splitter.call_args_list]
260+
assert 0 in idxs
261+
262+
237263
@pytest.mark.parametrize("split_collections", [False, True])
238264
@image_comparison(['contour_corner_mask_False.png', 'contour_corner_mask_True.png'],
239265
remove_text=True, tol=1.88)

0 commit comments

Comments
 (0)