Q:

Check if object is visible (with current xlim / ylim)

I am creating a boxplot were I need to zoom into some part to show relevant details. Now I want to draw an arrow for each box which is not in the field of view.
Is there some automatic way to iterate through the boxes and check if they are currently visible?
I know I can get all box data with
bp = plt.boxplot([[2, 3], [0, 1]])
plt.ylim([-1, 2])  # first box not visible

# all boxes
print(bp['boxes'])

# all whiskers
print(bp['fliers'])

but how can I check if they are currently visible?

A:

By default, plt.boxplot() returns Line2D artists for the boxes and the whiskers. You can get the coordinates of the Line2D points using Line2D.get_{x|y}data(). Therefore, you can easily test whether the points are between the limits of the axes.
def is_visible(line, xmin, xmax, ymin, ymax):
    x,y = line.get_data()
    return (x.max()>xmin and x.min()<xmax) and (y.min()>ymin and y.max()<ymax)

for b in bp['boxes']:
    print(is_visible(b, *plt.gca().get_xlim(), *plt.gca().get_ylim()))

You'll have to decide for yourself whether having just part of the boxplot visible, or part of the whiskers visible should be considered as "visible" or not.

