Skip to content

Axis ticks are not aligned by baseline #4115

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

Closed
jowr opened this issue Feb 17, 2015 · 11 comments
Closed

Axis ticks are not aligned by baseline #4115

jowr opened this issue Feb 17, 2015 · 11 comments
Labels
backend: pgf status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action

Comments

@jowr
Copy link
Contributor

jowr commented Feb 17, 2015

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.

@tacaswell tacaswell added this to the next point release milestone Feb 17, 2015
@tacaswell
Copy link
Member

cc @pwuertz

@pwuertz pwuertz self-assigned this Feb 17, 2015
@pwuertz
Copy link
Contributor

pwuertz commented Feb 17, 2015

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).

@pwuertz
Copy link
Contributor

pwuertz commented Feb 18, 2015

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.

@pwuertz
Copy link
Contributor

pwuertz commented Feb 18, 2015

@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)

@pwuertz pwuertz changed the title PGF backend axis tick alignment Axis ticks are not aligned by baseline Feb 18, 2015
@jowr
Copy link
Contributor Author

jowr commented Feb 18, 2015

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!

@jowr
Copy link
Contributor Author

jowr commented Feb 18, 2015

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...

@pwuertz
Copy link
Contributor

pwuertz commented Feb 18, 2015

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.

@tacaswell tacaswell reopened this Feb 18, 2015
@tacaswell
Copy link
Member

@jowr Can you put that into a pull request?

@pwuertz Re-opened

@pwuertz
Copy link
Contributor

pwuertz commented Feb 18, 2015

@tacaswell: This is only a workaround, I don't think this is ready for a PR yet. We need to start/continue a discussion on how to implement base-alignment by default and how to setup the necessary padding, i.e. which usecases are to be covered, which pathological cases are to be ignored.

@pwuertz pwuertz removed their assignment Feb 18, 2015
@tacaswell tacaswell modified the milestones: next point release, Color overhaul Jul 17, 2015
@tacaswell tacaswell modified the milestones: Color overhaul, next major release (2.0) Oct 26, 2015
@tacaswell tacaswell modified the milestones: 2.0.1 (next bug fix release), 2.0 (style change major release) Mar 21, 2016
@mdboom mdboom modified the milestones: 2.0 (style change major release), 2.0.1 (next bug fix release) Mar 21, 2016
@QuLogic QuLogic modified the milestones: 2.0.1 (next bug fix release), 2.0.2 (next bug fix release) May 3, 2017
@tacaswell tacaswell modified the milestones: 2.1.1 (next bug fix release), 2.2 (next feature release) Oct 9, 2017
@github-actions
Copy link

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Mar 11, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 11, 2023
@rcomer rcomer added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label May 30, 2023
@zlee-personal
Copy link

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
Labels
backend: pgf status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action
Projects
None yet
Development

No branches or pull requests

7 participants