Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Clip box property of ax.text(...) has no effect #30718

@leofels

Description

@leofels

Bug summary

Setting the clip_box property of text generated with ax.text(...) has no effect, regardless of whether it is set directly during generation with the clip_box parameter or afterwards with the setter function text.set_clip_box(...).

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox, TransformedBbox

# Create data for the plot
x = range(10)
y = range(10)

# Create the plot
fig, ax = plt.subplots()

# Create a bounding box
bbox = Bbox([[5, 2], [7, 8]])
# Transform the bounding box to the coordinate system of the data
tbbox = TransformedBbox(bbox, ax.transData)

# Draw the bounding box on the plot for better understanding
rect = plt.Rectangle((bbox.xmin, bbox.ymin), bbox.width, bbox.height, fill=False, edgecolor='r', linewidth=2)
ax.add_patch(rect)

# Plot a line and a text
line, = ax.plot(x, y, color='blue')
text = ax.text(5, 5, "Lorem ipsum dolor sit amet.")

# Apply the bounding box to the line and text
line.set_clip_box(tbbox)
text.set_clip_box(tbbox)

# Show the plot
plt.show()

Actual outcome

While the line drawn with ax.plot(...) is correctly clipped, the text is not clipped:
Image

Expected outcome

The text should be clipped in the same way as the line:
Image

Additional information

The same problem occurs if you use text = ax.text(5, 5, "Lorem ipsum dolor sit amet.", clip_box=tbbox) in line 22 instead of line 26.

Operating system

Windows 11

Matplotlib Version

3.10.7

Matplotlib Backend

module://backend_interagg

Python version

3.14.0

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions