Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: set_position for tick labels don't work #30056

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
AntonyCotton opened this issue May 15, 2025 · 1 comment
Closed

[Bug]: set_position for tick labels don't work #30056

AntonyCotton opened this issue May 15, 2025 · 1 comment

Comments

@AntonyCotton
Copy link

Bug summary

Sometimes, the axis tick labels overlaps. In such cases, manually adjusting their positions slightly is one solution. However, I found that this doesn't seem to work.

Here is a simple example. I simulated a situation where the text would overlap by setting a smaller figure size.

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(0.2, 0.2))
ax = fig.subplots(1, 1)
ax.plot(range(10), range(10))
plt.show()

As it can be seen, the y-axis labels '0' and '5' have some overlap.

Image

Then, I obtained the y-axis tick label objects using get_yticklabels and tried to raise the position of '5' using set_position. As shown below, nothing changed.

Code for reproduction

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(0.2, 0.2))
ax = fig.subplots(1, 1)
ax.plot(range(10), range(10))
texts = ax.get_yticklabels()
for text in texts:
    if text.get_text() == "5":
        text.set_position((0, 6))
plt.show()

Actual outcome

Image

Expected outcome

The position of '5' on the y-axis should be adjusted upwards.

Additional information

No response

Operating system

Debian

Matplotlib Version

3.10.1

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.13.2

Jupyter version

4.4.0

Installation

pip

@timhoffm
Copy link
Member

Ticks (and their labels) are dynamically positioned through the locator (and possibly updated). Trying to manually move a tick label is fighting against this mechanism. I won't exclude that you may be able to achieve this with the right set of commands, but it's non trivially depending on matplotlib internals and I strongly advise against doing that.

Such label overlap should only happen in extremly small plots and is therefore a very edge case. Better alternatives include

  • explicitly set other tick positions
  • use a slightly smaller font
  • if you really must: text.set_visible(False) and manually place a new text at the desired position.

@timhoffm timhoffm closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2025
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

2 participants