Skip to content
  • Sponsor matplotlib/matplotlib

  • Notifications You must be signed in to change notification settings
  • Fork 7.9k

pad=0 leaves space between label and tick #20677

Open
@nschloe

Description

@nschloe

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

Activity

tzeitim

tzeitim commented on Mar 21, 2023

@tzeitim

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

timhoffm commented on Sep 25, 2024

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

ksunden commented on Sep 25, 2024

@ksunden
Member

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pad=0 leaves space between label and tick · Issue #20677 · matplotlib/matplotlib