python - Matplotlib mathtext not rendering left and right brackets correctly with superscripts - Stack Overflow

admin2025-04-15  0

I'm trying to include units in text in a plot, and those units can be squared so the brackets need to be resized so they fully encompass the height of the superscript.

Here is a minimal working example:

import matplotlib.pyplot as plt

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

ax.set_xlabel(r'Area $\left[ \mathrm{cm}^{2}\right]$')

plt.savefig('figure.png')

Which produces this image, where you can see that the brackets are too small for the text. The brackets do increase in size when the superscript is added, but clearly not enough to actually encompass the whole thing.

Note that if I make the label include a fraction of some kind, the \left[ and \right] work to scale the brackets (although it's still not perfect), and even when putting a superscript inside the bracket, it works.

I don't want to switch to using full LaTeX rendering if I don't have to.

EDIT: It's most likely a bug rather than a setting.

转载请注明原文地址:http://www.anycun.com/QandA/1744714709a86609.html