Skip to content

Fix: Vertical Misalignment of LaTeX X-tick Labels#31891

Open
jayaprajapatii wants to merge 1 commit into
matplotlib:mainfrom
jayaprajapatii:fix-latex-xtick-dpi-alignment
Open

Fix: Vertical Misalignment of LaTeX X-tick Labels#31891
jayaprajapatii wants to merge 1 commit into
matplotlib:mainfrom
jayaprajapatii:fix-latex-xtick-dpi-alignment

Conversation

@jayaprajapatii

Copy link
Copy Markdown
Contributor

closes #31802

PR Summary

x-tick labels using LaTeX/mathtext were vertically misaligned when savefig(dpi=300) was used, specifically when labels had different "ascents" (height above baseline) due to differing mathtext content — e.g. $w^{(2)}_1$ (superscript + subscript) vs $b^{(2)}$ (superscript only).

Why is this change necessary?

All x-tick label1s are top-aligned (va='top') to the same y-position via _get_text1_transform(). Since baseline_y = y1 - ascent, labels with different ascents end up with different baseline positions even though their top edges align. This difference is small but becomes visually noticeable at high DPI.

What problem does it solve?

It fixes the vertical misalignment of LaTeX x-tick labels, where one label (with a smaller ascent) appeared visually higher than the others.

Reason for this implementation?

Added Axis._align_xtick_label_baselines(), called from Axis.draw() after _update_ticks(). For each visible x-tick label1:

  1. Measure its ascent via label._get_layout(renderer).
  2. Find max_ascent across all labels.
  3. Shift labels with smaller ascent down by (max_ascent - ascent), so all baselines align with the label that has the largest ascent.
  4. Added Regression test.

NOTE: A 0.5px threshold skips labels whose ascent differs only by sub-pixel amounts, so plain numeric/text tick labels (where this difference is negligible) remain unaffected.

AI Disclosure

AI was used in documentation build and suggesting validation scenarios.
All Implementation, testing, and verification were done manually.

PR checklist

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant