Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f93091e

Browse files
Simplified search for plot datat index matching mouse cursor position
Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent 5319e89 commit f93091e

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

examples/widgets/annotated_cursor.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,16 @@ def setpos(self, xpos, ypos):
240240
raise ValueError(f"The data axis specifier {self.dataaxis} should "
241241
f"be 'x' or 'y'")
242242

243-
# If position is valid
244-
if pos is not None:
245-
# And in valid plot data range
246-
if pos >= lim[0] and pos <= lim[-1]:
247-
# Convert given positon to numpy array,
248-
# so numpy function can be used.
249-
findme = np.array([pos])
250-
# Find closest x value in sorted x vector.
251-
# This is the code line,
252-
# which requires the plotted data to be sorted.
253-
index = np.searchsorted(data, findme)
254-
# Return none, if this index is out of range.
255-
if (index < 0) or (index >= len(data)):
256-
return None
257-
# Return plot point as tuple.
258-
return (xdata[index][0], ydata[index][0])
243+
# If position is valid and in valid plot data range.
244+
if pos is not None and lim[0] <= pos <= lim[-1]:
245+
# Find closest x value in sorted x vector.
246+
# This requires the plotted data to be sorted.
247+
index = np.searchsorted(data, pos)
248+
# Return none, if this index is out of range.
249+
if index < 0 or index >= len(data):
250+
return None
251+
# Return plot point as tuple.
252+
return (xdata[index][0], ydata[index][0])
259253

260254
# Return none if there is no good related point for this x position.
261255
return None

0 commit comments

Comments
 (0)