matplotlib text only in plot area

matplotlib, python

Solution

You just need to tell the text artists to not clip:

txt = ax.text(...)

txt.set_clip_on(False)  # this will turn clipping off (always visible)
# txt.set_clip_on(True) # this will turn clipping on (only visible when text in data range)

However, there is a bug matplotlib (https://github.com/matplotlib/matplotlib/pull/1885 now fixed) which makes this not work. The other way to do this (as mentioned in the comments) is to use

txt = ax.text(..., clip_on=True)

Problem

I use the matplotlib library for plotting data in python. In my figure I also have some text to distinguish the data. The problem is that the text goes over the border in the figure window. Is it possible to make the border of the plot cut off the text at the corresponding position and only when I pan inside the plot the the rest of the text gets visible (but only when inside plot area). I use the text() function to display the text [EDIT:] The code looks like this: ``` fig = plt.figure() ax = fig.add_subplot(111) # ... txt = ax.text(x, y, n, fontsize=10) txt.set_clip_on(False) # I added this due to the answer from tcaswell ```

Original source

Related problems