Skip to content

Commit cff30c0

Browse files
authored
Merge pull request #213 from scottshambaugh/single_element_lines
Fix labels on single point lines
2 parents 2533a61 + 9524e3b commit cff30c0

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed
13.4 KB
Loading

labellines/line_label.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,18 @@ def _update_anchors(self):
192192
xdata = xdata[mask]
193193
ydata = ydata[mask]
194194

195-
# Find the first line segment surrounding x
196-
for i, (xa, xb) in enumerate(zip(xdata[:-1], xdata[1:])):
197-
if min(xa, xb) <= x <= max(xa, xb):
198-
ya, yb = ydata[i], ydata[i + 1]
199-
break
195+
# If the valid data is a single point, then just use that point
196+
if len(xdata) == 1:
197+
xa, xb = xdata[0], xdata[0]
198+
ya, yb = ydata[0], ydata[0]
200199
else:
201-
raise ValueError("x label location is outside data range!")
200+
# Find the first line segment surrounding x
201+
for i, (xa, xb) in enumerate(zip(xdata[:-1], xdata[1:])):
202+
if min(xa, xb) <= x <= max(xa, xb):
203+
ya, yb = ydata[i], ydata[i + 1]
204+
break
205+
else:
206+
raise ValueError("x label location is outside data range!")
202207

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

labellines/test.py

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

381381

382+
@pytest.mark.mpl_image_compare
383+
def test_single_point_line(setup_mpl):
384+
plt.plot(1, 1, label="x")
385+
labelLines(plt.gca().get_lines())
386+
return plt.gcf()
387+
388+
382389
def test_warning_out_of_range():
383390
X = [0, 1]
384391
Y = [0, 1]

0 commit comments

Comments
 (0)