Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
I first thank all Matplotlib developers and contributors again for maintaining the software!
I would like to report a potential inconsistencies between normal text and math text; specifically:
- Normal text and math text do not have the same bbox even for the essentially same string.
- Math text seems to be offset from the center of its bbox and thus the expected position.
- The shape of the bbox and the offset for math text seems to be backend-dependent.
Code for reproduction
#!/usr/bin/env pythonimportmatplotlib.pyplotaspltfrommatplotlib.tickerimportMultipleLocatordefdecorate_ax(ax):ax.set_xlim(-0.5,9.5)ax.set_ylim(-0.5,1.5)ax.xaxis.set_major_locator(MultipleLocator(1))ax.yaxis.set_major_locator(MultipleLocator(1))ax.grid(which="both")ax.tick_params(tick1On=False,tick2On=False,labelbottom=False,labelleft=False)bbox=dict(ec="none",fc="C1",ls="none",lw=0.0,pad=0.0)kwargs=dict(ha="center",va="center",ma="center",bbox=bbox)fig,ax=plt.subplots(figsize=(1.6,0.4))fig.subplots_adjust(left=0.025,right=0.975,bottom=0.05,top=0.95)decorate_ax(ax)foriinrange(10):ax.text(i,0,f"{i}",**kwargs)ax.text(i,1,f"$\mathrm{{{i}}}$",**kwargs)fig.savefig("test.png",transparent=True,dpi=300)fig.savefig("test.pdf",transparent=True)plt.close()
Actual outcome
The code above makes the following figure for PDF (afterwords converted into PNG using ImageMagick).
The bottom row shows normal text, and the top row shows math text.
While the normal text is at the center of the bbox, the math text is offset towards upper left.
Further, the bbox of math text is slightly wider than the bbox of normal text.
The behavior of math text seems also backend-dependent. The below is the figure in the PNG backend.
Oppositely to the PDF backend above, the bbox of math text is slightly narrower than the bbox of normal text, and the text seems to be offset towards right.
The ZIP file below contains the script and the figures made.
Expected outcome
- It would be ideal if the normal and the math text shows exactly the same positioning and the bbox for simple text, i.e., without super/subscripts and without math symbols.
- It would be nice if the behavior becomes backend independent.
The behavior above is a bit inconvenient for me when making a figure for production. Particularly, when I need to write super/subscripts, I need to use the math-text mode. As demonstrated above, however, this makes the text slightly offset from the expected position.
I am looking forward to your kind feedback. Thank you so much for your help in advance.
Additional information
No response
Operating system
OS/X
Matplotlib Version
3.10.0
Matplotlib Backend
MacOSX
Python version
Python 3.11.8
Jupyter version
No response
Installation
conda