You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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")
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).
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.
@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)
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!
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.
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!
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.
Activity
tacaswell commentedon Feb 17, 2015
cc @pwuertz
pwuertz commentedon 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 commentedon 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 commentedon Feb 18, 2015
@jowr: As a workaround, you could manually base-align like you did already and set an appropriate padding for the labels:
[-]PGF backend axis tick alignment[/-][+]Axis ticks are not aligned by baseline[/+]jowr commentedon 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 commentedon Feb 18, 2015
Thanks for the quick fix. I currently use this code:
since I read somewhere that one should reassign the text after changing the padding...
pwuertz commentedon 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.
15 remaining items
github-actions commentedon Mar 11, 2023
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!
zlee-personal commentedon Oct 24, 2023
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.