Skip to content

Commit a5152cf

Browse files
committed
Fix label_line_ends behaviour when lines go past right xlim
1 parent a0b7195 commit a5152cf

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

niceplots/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,16 @@ def label_line_ends(ax, lines=None, labels=None, colors=None, x_offset_pts=6, y_
303303
annotations = []
304304

305305
for line, label, color in zip(lines, labels, colors):
306-
# Get the x, y coordinates of the right-most point on the line
307-
maxXIndex = np.argmax(line.get_xdata())
308-
x = line.get_xdata()[maxXIndex]
309-
y = line.get_ydata()[maxXIndex]
306+
# Get the x, y coordinates of the right-most point on the line, or the right xlim of the axes if that is lower
307+
xData = line.get_xdata()
308+
yData = line.get_ydata()
309+
maxXIndex = np.argmax(xData)
310+
x = xData[maxXIndex]
311+
y = yData[maxXIndex]
312+
rightXlim = ax.get_xlim()[1]
313+
if x > rightXlim:
314+
x = rightXlim
315+
y = yData[np.argmax(xData >= x)]
310316
annote = ax.annotate(
311317
label,
312318
xy=(x, y),

0 commit comments

Comments
 (0)