matplotlib text not clipped

2024/11/17 17:43:07

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 pltplt.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!

Answer

This behavior can be controled by the kwarg clip_on:

import matplotlib.pyplot as pltplt.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.

https://en.xdnf.cn/q/71192.html

Related Q&A

how to make child class call parent class __init__ automatically?

i had a class called CacheObject,and many class extend from it.now i need to add something common on all classes from this class so i write thisclass CacheObject(object):def __init__(self):self.updated…

Creating a dataframe in pandas by multiplying two series together

Say I have two series in pandas, series A and series B. How do I create a dataframe in which all of those values are multiplied together, i.e. with series A down the left hand side and series B along t…

UnicodeDecodeError in PyCharm debugger

Its a reference to UnicodeDecodeError while using cyryllic .I have same problem with Python 3.3 and Pycharm 2.7.2 Tryed to hardcode encoding in code, manually specifying encoding in Pycharm options, bu…

Scipy griddata with linear and cubic yields nan

the following code should produce griddata. But in case I choose as interpolation type cubic or linear I am getting nans in the z grid. Wen im choosing nearest everything is running fine. Here is an ex…

Clone a module and make changes to the copy

Is it possible to copy a module, and then make changes to the copy? To phrase another way, can I inherit from a module, and then override or modify parts of it?

AWS Lambda, Python, Numpy and others as Layers

I have been going at this for a while trying to get python, numpy and pytz added to AWS Lambda as Layers rather than having to zip and throw it at AWS with my .py file. I was able to follow multiple tu…

Is there a way to check if a module is being loaded by multiprocessing standard module in Windows?

I believe on Windows, because there is no fork, the multiprocessing module reloads modules in new Pythons processes. You are required to have this code in your main script, otherwise very nasty crashes…

Condas solving environment takes forever

I am using conda since one year, since several weeks, whenever I want to install a package using conda install -c anaconda <package_name>, for any package, it is just stuck at the Solving environ…

How to overwrite a file in Python?

Im trying to overwrite a file. I based my answer on this Read and overwrite a file in PythonTo complete my codes:<select class="select compact expandable-list check-list" ONCHANGE="lo…

Is os.popen really deprecated in Python 2.6?

The on-line documentation states that os.popen is now deprecated. All other deprecated functions duly raise a DeprecationWarning. For instance:>>> import os >>> [c.close() for c in os…