Skip to content

pad=0 leaves space between label and tick #20677

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

Open
nschloe opened this issue Jul 19, 2021 · 3 comments
Open

pad=0 leaves space between label and tick #20677

nschloe opened this issue Jul 19, 2021 · 3 comments

Comments

@nschloe
Copy link
Contributor

nschloe commented Jul 19, 2021

I would like to place the tick labels snug with the ticks. Setting pad=0 is not enough: There is still a distance between the label and the tick. This is with matplotlib 3.4.2. MWE:

import matplotlib.pyplot as plt
import numpy as np


x = np.array([0.0, 1.0])
y = x
plt.plot(x, y)

ax = plt.gca()
ax.tick_params(axis='both', which='major', pad=0, length=10, width=20)
plt.show()

s3

s1

s2

@tzeitim
Copy link

tzeitim commented Mar 21, 2023

I workaround I used is to set the pad to negative values. I am guessing that the origin of the vector is at the geometric center. Perhaps if it can be controlled via a 'left-justified' of 'right-justified' the label could be better controlled.

@timhoffm
Copy link
Member

This is a general issue with the determination of text sizes (related #13044), as can seen by drawing boxes around the tick labels:

import matplotlib.pyplot as plt
import numpy as np


x = np.array([0.0, 1.0])
y = x
plt.plot(x, y)

ax = plt.gca()
ax.tick_params(axis='both', which='major', pad=0, length=10, width=20)

box_style = dict(facecolor='None', edgecolor='red', pad=0)
for tick in ax.xaxis.get_major_ticks():
    tick.label1.set_bbox(box_style)
for tick in ax.yaxis.get_major_ticks():
    tick.label1.set_bbox(box_style)

plt.show()

image

@ksunden
Copy link
Member

ksunden commented Sep 25, 2024

You can get at least closer by using text.usetex=True, as text rendered using LaTeX uses a relatively tight bounding box. (Quick test was still not perfect, but only a pixel or two between)

It is an imperfect solution, for several reasons, but noting here just in case someone finds it useful.

One of the more subtle reasons it is imperfect is because the reason this may work for this usecase is actually the cause of other problems such as #28766 and #24166, where positioning certain rendered text artifacts is hard to contain (e.g. cutting out whitespace or problems with vertical position of dashes)

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

No branches or pull requests

5 participants