diff --git a/labellines/baseline/test_single_point_line.png b/labellines/baseline/test_single_point_line.png new file mode 100644 index 0000000..2d13752 Binary files /dev/null and b/labellines/baseline/test_single_point_line.png differ diff --git a/labellines/line_label.py b/labellines/line_label.py index 41a4e6a..a0b4c72 100644 --- a/labellines/line_label.py +++ b/labellines/line_label.py @@ -168,13 +168,18 @@ def _update_anchors(self): xdata = xdata[mask] ydata = ydata[mask] - # Find the first line segment surrounding x - for i, (xa, xb) in enumerate(zip(xdata[:-1], xdata[1:])): - if min(xa, xb) <= x <= max(xa, xb): - ya, yb = ydata[i], ydata[i + 1] - break + # If the valid data is a single point, then just use that point + if len(xdata) == 1: + xa, xb = xdata[0], xdata[0] + ya, yb = ydata[0], ydata[0] else: - raise ValueError("x label location is outside data range!") + # Find the first line segment surrounding x + for i, (xa, xb) in enumerate(zip(xdata[:-1], xdata[1:])): + if min(xa, xb) <= x <= max(xa, xb): + ya, yb = ydata[i], ydata[i + 1] + break + else: + raise ValueError("x label location is outside data range!") # Interpolate y position of label, (interp needs sorted data) if xa != xb: diff --git a/labellines/test.py b/labellines/test.py index e317148..7e22ed0 100644 --- a/labellines/test.py +++ b/labellines/test.py @@ -361,6 +361,13 @@ def test_auto_layout(setup_mpl): return plt.gcf() +@pytest.mark.mpl_image_compare +def test_single_point_line(setup_mpl): + plt.plot(1, 1, label="x") + labelLines(plt.gca().get_lines()) + return plt.gcf() + + def test_warning_out_of_range(): X = [0, 1] Y = [0, 1]