GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Bug]: LaTeX X-Tick Labels are Vertically Misaligned When DPI is Set · Issue #31802 · matplotlib/matplotlib

[Bug]: LaTeX X-Tick Labels are Vertically Misaligned When DPI is Set #31802

New issue
Copy link
New issue
Copy link
Open
#31891
Open
[Bug]: LaTeX X-Tick Labels are Vertically Misaligned When DPI is Set#31802
#31891
Copy link
Labels
first-contributiontopic: texttopic: text/mathtexttopic: ticks axis labels

Description

@mikewacker
mikewacker
opened on Jun 2, 2026
Issue body actions

Bug summary

x-tick labels using LaTeX are vertically misaligned when you call savefig() with dpi=300. The misalignment occurs when two labels have a superscript and subscript, while the third misaligned label only has a superscript.

Code for reproduction

import sys

import matplotlib
import matplotlib.pyplot as plt

print(matplotlib.__version__)
print(matplotlib.get_backend())
print(sys.version)

idxs = list(range(3))
labels = ["$w^{(2)}_1$", "$w^{(2)}_2$", "$b^{(2)}$"]

fig, ax = plt.subplots(figsize=(3, 3))
ax.set_xlim(-0.5, 2.5)
ax.set_ylim(-3, 3)
ax.set_xticks(idxs, labels)
ax.set_yticks([])
ax.tick_params(which="both", length=0)
fig.tight_layout()
fig.savefig("test.png", dpi=300)

Actual outcome

Here is the image produced by the code. You can see that the $b^{(2)}$ label is vertically misaligned; it's higher than the other two labels.

Image

Expected outcome

Here is the same image if you run the code without the dpi arg in savefig(). You can see that the $b^{(2)}$ label is vertically aligned with the other two labels.

Image

Additional information

[EDIT: slight fix to code] Here's the reproduction code modified to use a workaround:

import sys

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.artist import Artist
from matplotlib.figure import Figure

print(matplotlib.__version__)
print(matplotlib.get_backend())
print(sys.version)


def get_center(fig: Figure, artist: Artist) -> tuple[float, float]:
    renderer = fig.canvas.get_renderer()  # type: ignore  # get_renderer() not in base class
    bbox = artist.get_window_extent(renderer)
    fig_box = bbox.transformed(fig.transFigure.inverted())
    x = (fig_box.xmin + fig_box.xmax) / 2
    y = (fig_box.ymin + fig_box.ymax) / 2
    return x, y


idxs = list(range(3))
labels = ["$w^{(2)}_1$", "$w^{(2)}_2$", "$b^{(2)}_1$"]  # add subscript to b2

fig, ax = plt.subplots(figsize=(3, 3))
ax.set_xlim(-0.5, 2.5)
ax.set_ylim(-3, 3)
ax.set_xticks(idxs, labels)
ax.set_yticks([])
ax.tick_params(which="both", length=0)
fig.tight_layout()  # call the layout function before fixing the label

orig_b2_label = ax.get_xticklabels()[-1]
orig_b2_x, orig_b2_y = get_center(fig, orig_b2_label)  # measure while it's still visible
orig_b2_label.set_visible(False)
b2_label = fig.text(
    orig_b2_x, orig_b2_y, "$b^{(2)}$", ha="center", va="center", transform=fig.transFigure
)
b2_label.set_in_layout(False)

fig.savefig("test-workaround.png", dpi=300)

Here is the image with the workaround:

Image

(Feel free to let me know if there is a better workaround.)

Operating system

Ubuntu 24

Matplotlib Version

3.10.9

Matplotlib Backend

module://backend_interagg

Python version

Python 3.12.3

Jupyter version

No response

Installation

uv

Activity

  1. github-actions
    added
    first-contribution
    on Jun 2, 2026
  2. QuLogic commented on Jun 2, 2026

    @QuLogic
    QuLogic
    on Jun 2, 2026
    Member
    More actions

    The problem here is that the x-ticks are aligned to the top, but they are all independent of each other. Ideally, they would be aligned to the baseline, with some adjustment for the height of the largest one. As they do not know about each other, there is no way for this to happen right now.

    A simple, but possibly fragile, workaround would be to increase the padding on the xticks to fit the largest text you have, then manually change the text alignment to baseline. However, as ticks can be replaced at any time, it is important that you do this at the last possible moment.

    import sys
    
    import matplotlib
    import matplotlib.pyplot as plt
    
    idxs = list(range(3))
    labels = ["$w^{(2)}_1$", "$w^{(2)}_2$", "$w^{(2)}$"]
    
    plt.rcParams['xtick.major.pad'] = 14  # Make this large enough to fit all your text vertically.
    
    fig, ax = plt.subplots(figsize=(3, 3))
    ax.set_xlim(-0.5, 2.5)
    ax.set_ylim(-3, 3)
    ax.set_xticks(idxs, labels)
    ax.set_yticks([])
    ax.tick_params(which="both", length=0)
    fig.tight_layout()
    for tick in ax.xaxis.get_major_ticks():
        tick.label1.set_verticalalignment('baseline')  # Now set alignment at the baseline at that padded position.
    fig.savefig("test.png", dpi=300)
    Image
  3. scottshambaugh
    added
    topic: text
    topic: text/mathtext
    on Jun 2, 2026
  4. QuLogic
    added
    topic: ticks axis labels
    on Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    first-contributiontopic: texttopic: text/mathtexttopic: ticks axis labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.