Skip to content

Commit 7d13a70

Browse files
committed
FIX: labels at start of contours
1 parent 3044bde commit 7d13a70

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
@@ -233,6 +234,31 @@ def test_labels(split_collections):
233234
_maybe_split_collections(split_collections)
234235

235236

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

0 commit comments

Comments
 (0)