Description
Bug summary
I’m trying to use a constrained layout in a visualization that contains an artist that is an instance of AnnotationBbox, and matplotlib raises a warning saying constrained layout is not applied. The visual effect is not evident in this simple example, but it becomes very clear once we have multiple panels.
Code for reproduction
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnnotationBbox, TextArea
fig, ax = plt.subplots(layout="constrained")
ab = AnnotationBbox(
TextArea("Some text", textprops={"size": 42}),
(0.5, 0.5),
xycoords="axes fraction",
box_alignment=(0.5, 0.5),
pad=0
)
ax.add_artist(ab)
fig.set_facecolor("w")
fig.savefig("annotation_box.png", dpi=300)
Actual outcome
UserWarning: constrained_layout not applied because axes sizes collapsed to zero. Try making figure larger or axes decorations smaller.
Expected outcome
No warning should appear
Additional information
The following works without any warning
fig, ax = plt.subplots(layout="constrained")
ax.text(0.5, 0.5, "Some text", size=42, ha="center")
fig.set_facecolor("w")
fig.savefig("ax_text.png", dpi=300)
The problem with the constrained layout is more evident if I have two or more panels.
One way of fixing it (i.e. getting rid of the warning and bad functionality) is to do ab.set_in_layout(False) before doing ax.add_artist(ab).
This was first posted on Discourse https://discourse.matplotlib.org/t/constrained-layout-does-not-work-well-with-annotationbbox/23301
Operating system
Ubuntu 22
Matplotlib Version
3.6.2
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
pip