I’m trying to improve the bounding box capabilities of Flexitext and I’m facing one issue with the height of the bounding box of the text.
Let’s see the following example
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 3))
fig.set_facecolor("w")
ax.text(0.5, 0.5, "abcde", size=60, ha="right", bbox={"fc": "green", "pad": 0})
ax.text(0.5, 0.5, "fghijk", size=60, ha="left", bbox={"fc": "green", "pad": 0})
fig.savefig("plot.png", dpi=300)
In both cases, Matplotlib created a bounding box of the same height. On the left, this results in extra space below abcde
. On the right, this does not result in any extra space, because it is used by letters such as g
and j
.
My guess is that Matplotlib looks at the height of all the glyphs in the font being used, and makes sure anything you write fits in the bounding box.
I would like to know if it is possible to adjust the height of the bounding box to the actual text, without considering other glyphs in the font.
On the other hand, I have another example where the text escapes the bounding box.
fig, ax = plt.subplots(figsize=(8, 3))
fig.set_facecolor("w")
ax.text(0.5, 0.5, "abcde", size=60, ha="center", fontname="Satisfy", bbox={"fc": "green", "pad": 0})
fig.savefig("plot.png", dpi=300)
Is this a bug in how Matplotlib determines the size of the bounding box?