Skip to content

Commit 7950370

Browse files
committed
Generalise for x and y limits
1 parent a5152cf commit 7950370

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

niceplots/utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,22 @@ 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, or the right xlim of the axes if that is lower
306+
# Get the x, y coordinates of the right-most point on the line that is within the axis limits
307307
xData = line.get_xdata()
308308
yData = line.get_ydata()
309+
xlim = ax.get_xlim()
310+
ylim = ax.get_ylim()
311+
312+
pointsInXLimits = np.logical_and(xData >= xlim[0], xData <= xlim[1])
313+
pointsInYLimits = np.logical_and(yData >= ylim[0], yData <= ylim[1])
314+
pointsInLimits = np.logical_and(pointsInXLimits, pointsInYLimits)
315+
xData = xData[pointsInLimits]
316+
yData = yData[pointsInLimits]
317+
309318
maxXIndex = np.argmax(xData)
310319
x = xData[maxXIndex]
311320
y = yData[maxXIndex]
312-
rightXlim = ax.get_xlim()[1]
313-
if x > rightXlim:
314-
x = rightXlim
315-
y = yData[np.argmax(xData >= x)]
321+
316322
annote = ax.annotate(
317323
label,
318324
xy=(x, y),

0 commit comments

Comments
 (0)