8000 [Bug]: Position and BBox of mathtext are slightly offset from the expected ones and inconsistent with normal text · Issue #29596 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yuzie007 opened this issue Feb 9, 2025 · 1 comment

Comments

@yuzie007
Copy link
Contributor
yuzie007 commented Feb 9, 2025

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 python
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator


def decorate_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)
for i in range(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

@scottshambaugh
Copy link
Contributor

Potentially related to #13044?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
0