Skip to content

Commit fe290aa

Browse files
authored
Merge pull request #211 from scottshambaugh/nan_mask
Fix error when trying to plot labels in a gap in the data
2 parents 7860fb3 + 764fd2c commit fe290aa

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

labellines/baseline/test_nan_gaps.png

111 KB
Loading

labellines/line_label.py

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ def _update_anchors(self):
165165
mask = np.isfinite(ydata)
166166
if mask.sum() == 0:
167167
raise ValueError(f"The line {self._line} only contains nan!")
168+
xdata = xdata[mask]
169+
ydata = ydata[mask]
168170

169171
# Find the first line segment surrounding x
170172
for i, (xa, xb) in enumerate(zip(xdata[:-1], xdata[1:])):

labellines/test.py

+14
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ def test_nan_failure():
243243
labelLine(line, 0.5)
244244

245245

246+
@pytest.mark.mpl_image_compare
247+
def test_nan_gaps(setup_mpl):
248+
x = np.linspace(0, 10, 100)
249+
x[10:30] = np.nan
250+
251+
for i in range(10):
252+
y = np.sin(x + i)
253+
plt.plot(x, y, label=f"y={i}")
254+
255+
labelLines(plt.gca().get_lines())
256+
257+
return plt.gcf()
258+
259+
246260
@pytest.mark.mpl_image_compare
247261
def test_label_range(setup_mpl):
248262
x = np.linspace(0, 1)

0 commit comments

Comments
 (0)