Skip to content

Commit 8619657

Browse files
Fix labels on single point lines
1 parent b17e58e commit 8619657

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

labellines/line_label.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,18 @@ def _update_anchors(self):
168168
xdata = xdata[mask]
169169
ydata = ydata[mask]
170170

171-
# Find the first line segment surrounding x
172-
for i, (xa, xb) in enumerate(zip(xdata[:-1], xdata[1:])):
173-
if min(xa, xb) <= x <= max(xa, xb):
174-
ya, yb = ydata[i], ydata[i + 1]
175-
break
171+
# If the valid data is a single point, then just use that point
172+
if len(xdata) == 1:
173+
xa, xb = xdata[0], xdata[0]
174+
ya, yb = ydata[0], ydata[0]
176175
else:
177-
raise ValueError("x label location is outside data range!")
176+
# Find the first line segment surrounding x
177+
for i, (xa, xb) in enumerate(zip(xdata[:-1], xdata[1:])):
178+
if min(xa, xb) <= x <= max(xa, xb):
179+
ya, yb = ydata[i], ydata[i + 1]
180+
break
181+
else:
182+
raise ValueError("x label location is outside data range!")
178183

179184
# Interpolate y position of label, (interp needs sorted data)
180185
if xa != xb:

labellines/test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ def test_auto_layout(setup_mpl):
361361
return plt.gcf()
362362

363363

364+
@pytest.mark.mpl_image_compare
365+
def test_single_point_line(setup_mpl):
366+
plt.plot(1, 1, label="x")
367+
labelLines(plt.gca().get_lines())
368+
return plt.gcf()
369+
370+
364371
def test_warning_out_of_range():
365372
X = [0, 1]
366373
Y = [0, 1]

0 commit comments

Comments
 (0)