I want to write text on a plot, but I noticed that when I move the horizontal position using matplotlib's panning (P key + mouse dragging) and the text is out of the plot area, it still appears. I would like it to disappear once its position exceeds the limits of the x-axis.
Here is a picture to make myself clearer:
The annotation on the right shouldn't be there. It should be present only if the position of the annotation is inside the x-axis.
Here's my code:
from matplotlib.pyplot import figure, show
import numpy as np
fig = figure()
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
x,y = np.random.rand(2,200)
ax.scatter(x,y)
ax.text(np.mean(x), np.max(y), 'A',
rotation = 0,
ha = 'center',
fontsize = 15,
bbox=dict(facecolor='yellow',edgecolor='black', boxstyle='round'))
show()
EDIT: Setting clip_on=True makes the annotation disappear, as it should be above the vertical axis maximum. On the following picture, the left shows what happens when that argument is set to True; on the right, the desired image.



text, like other artists, should have aclip_onargument, which, if set toTrue, would make the artist being clipped by the axes.clip_onargument clips the artist by both axes. Is it possible to do it just for the horizontal one? See updated post to see what I mean..set_clip_path. Here would be an example. One would need to use a blended transform for the path though. I will not be able to give a complete answer today, but if you haven't found a solution and no-one else did provide an answer until then feel free to remind me tomorrow.