Skip to content

Axis ticks are not aligned by baseline #4115

Closed as not planned
Closed as not planned

Description

@jowr

Hi, it looks like the PGF backend defaults to top-align tick labels without accounting for the height of the letters. This works without problems with the PDF backend, also using Tex to render text does not make a difference.
using this Python code:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(2,2))
ax = fig.add_subplot(111)
ax.plot([1,2],[5,7])
ax.set_xticks([1,2])
ax.set_xticklabels(["up","down"])
ax.set_xlim([0,3]) # get the ticks closer together
plt.tight_layout()
fig.savefig("ticks_std.pdf")
fig.savefig("ticks_std.pgf")
for lab in ax.xaxis.get_ticklabels():
    lab.set_verticalalignment("baseline")
fig.savefig("ticks_base.pdf")
fig.savefig("ticks_base.pgf")
for lab in ax.xaxis.get_ticklabels():
    lab.set_verticalalignment("top")
fig.savefig("ticks_top.pdf")
fig.savefig("ticks_top.pgf")

... and this Tex document:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tabular}{ccc}
\input{ticks_std.pgf} & \input{ticks_base.pgf} & \input{ticks_top.pgf} \\
\includegraphics{ticks_std.pdf} & \includegraphics{ticks_base.pdf} & \includegraphics{ticks_top.pdf}
\end{tabular}
\end{document} 

produces this:
ticks

One possible solution could be to use the baseline alignment with some additional padding depending on the font size.

Activity

  1. added this to the milestone on Feb 17, 2015
  2. tacaswell commented on Feb 17, 2015

    @tacaswell
    Member
  3. self-assigned this
    on Feb 17, 2015
  4. pwuertz commented on Feb 17, 2015

    @pwuertz
    Contributor

    I think I remember a discussion we once had about that. The point is that top-anchoring the individual ticks isn't the correct thing to do in the first place, but it is the default behaviour in matplotlib. The pgf backend is probably the only backend that reports the correct text sizes and thus shows this problem.

    Ideally one should look for the maximum height of all ticks and base-align all ticks at the same vertical position (or maybe just font-size + margin).

  5. pwuertz commented on Feb 18, 2015

    @pwuertz
    Contributor

    Ok, we talked about that problem a few times, for example in #1734. Apparently, issue #2051 was opened to discuss the necessary change in the default alignment, but it didn't really take off.

    I'm closing this one since it is a question of alignment not related to the backend.

  6. pwuertz commented on Feb 18, 2015

    @pwuertz
    Contributor

    @jowr: As a workaround, you could manually base-align like you did already and set an appropriate padding for the labels:

    for lab in ax.xaxis.get_ticklabels():
        lab.set_verticalalignment("baseline")
    fontsize = ax.xaxis.get_ticklabels()[0].get_size()
    ax.tick_params(axis='x', pad=fontsize+.8)
    
  7. changed the title [-]PGF backend axis tick alignment[/-] [+]Axis ticks are not aligned by baseline[/+] on Feb 18, 2015
  8. jowr commented on Feb 18, 2015

    @jowr
    ContributorAuthor

    Thank you all for the quick action. I indeed found the other issues, but I wanted to report a new one since I thought that the other ones were only looking at the axis label and not the ticks. Good luck with your work and keep up the good spirit for matplotlib!

  9. jowr commented on Feb 18, 2015

    @jowr
    ContributorAuthor

    Thanks for the quick fix. I currently use this code:

    for tick in ax1.get_xaxis().get_major_ticks():
        tick.set_pad(2*tick.get_pad())
        tick.label1 = tick._get_text1()
    

    since I read somewhere that one should reassign the text after changing the padding...

  10. pwuertz commented on Feb 18, 2015

    @pwuertz
    Contributor

    Ah, you are absolutely correct. The previous discussions were focused on labels, we shouldn't forget about the ticks. And yes, your quick fix looks good and might be more robust since you are forcing the tick positions to be re-evaluated.

  11. 15 remaining items

  12. modified the milestones: , on Mar 21, 2016
  13. modified the milestones: , on May 3, 2017
  14. modified the milestones: , on Oct 9, 2017
  15. github-actions commented on Mar 11, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  16. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on May 30, 2023
  17. zlee-personal commented on Oct 24, 2023

    @zlee-personal

    I'd note that the alignment can also be messed up from text rotation. A workaround I have found it to always use rotation_mode='anchor' when rotating text, despite this not normally being necessary in pdf mode. With this option the text alignment lab.set_horizontalalignment("right") seems to work as expected in pgf mode.

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

    backend: pgfstatus: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions