5
votes

When drawing text in matplotlib with text(), and then interactively panning the image, the resulting drawn text is not clipped to the data window. This is counter to how plotting data or drawing text using annotate() works, and doesn't make intuitive sense as text() uses data window coordinates.

import matplotlib.pyplot as plt

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)

ax.text(0.5, 0.2, 'text')
ax.annotate('anno', (0.5, 0.3))

plt.draw()

Interactively pan the text out of the data window on all sides. The annotate() drawn 'anno' is clipped when the reference point crosses the data window boundary, while the text() drawn 'text' is not.

I'm not sure if this behavior a feature or a bug, but sure seems like the latter, as this text interferes with axis labels, etc. Using 1.2.1 with TkAgg backend.

An additional question would be how to properly clip all text from going outside the data window, not just when the reference coordinate does.

Thanks!

1

1 Answers

12
votes

This behavior can be controled by the kwarg clip_on:

import matplotlib.pyplot as plt

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)

txt = ax.text(0.5, 0.2, 'text')
anno = ax.annotate('anno', (0.5, 0.3))
txt_clip = ax.text(0.5, 0.5, 'text-clip', clip_on=True)

plt.draw()

axes.text doc. There are arguments both for and against clipping the text to the data area.

There was a bug in mpl that made txt.set_clip_on(True) not work as expected.