Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Bug]: Position and BBox of mathtext are slightly offset from the expected ones and inconsistent with normal text #29596

Open
@yuzie007

Description

@yuzie007

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:

  1. Normal text and math text do not have the same bbox even for the essentially same string.
  2. Math text seems to be offset from the center of its bbox and thus the expected position.
  3. 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.

Image

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.

Image

The ZIP file below contains the script and the figures made.

mathtext.zip

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp