Skip to content

[Bug]: Setting ticks on log axis behaving unexpectedly #25894

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
Spl1x opened this issue May 16, 2023 · 2 comments
Closed

[Bug]: Setting ticks on log axis behaving unexpectedly #25894

Spl1x opened this issue May 16, 2023 · 2 comments
Labels
Community support Users in need of help.

Comments

@Spl1x
Copy link

Spl1x commented May 16, 2023

Bug summary

The issue occurs when formatting an axis (y-axis in this example) with 'log' scale and manually setting the limits. The process of setting the major and minor tick locations then does not work as expected/ as usual.

Code for reproduction

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# This section (1) fails to set the major ticks to locations [14, 18, 22, 26]
ax.set_yscale('log')
ax.set_ylim(13.5,26.5)
ax.set_yticks([14, 18, 22, 26])

# Adding this line results in section (1) working as expected
ax.set_yticks([], minor=True)

# Trying to also set the location of minor ticks leads to the same failure as in section (1)
ax.set_yticks([16, 16.5, 20, 24], minor=True)

# Adding this line leads to the expected output again
ax.set_yticklabels([], minor=True)

plt.show()

Actual outcome

Despite calling set_yticks in section (1), the actual y-ticks remain at their default location. This first part of the issue can be resolved by setting the minor tick locations to an empty list.
When also trying to set the location of minor ticks, however, it seems that labels are automatically created for these minor ticks.

Expected outcome

Calling set_yticks() should set the location of both major and minor ticks independently without the need of changing both at the same time or having to set the minor ticklabels to an empty list.

Additional information

No response

Operating system

Debian GNU/Linux 11

Matplotlib Version

3.5.1

Matplotlib Backend

QtAgg

Python version

3.9.2

Jupyter version

No response

Installation

pip

@ksunden
Copy link
Member

ksunden commented May 16, 2023

What is happening here is that when you are zoomed in to under one decade what you are actually seeing is the minor ticks which are labeled. (The major ticks actually still are [ 1., 10., 100., 1000.], just none of them happen to be inside of the view range)

If you only call ax.set_yticks(..., minor=True), I think you will find that it does what you expect. This is probably sufficient for a one off plot/non-interactive.

A bit more generally, this behavior is controlled by the minor_thresholds and labelOnlyBase parameters/attribute of the LogFormatter for the minor ticks.(ax.yaxis.get_minor_formatter())

ref: https://matplotlib.org/stable/api/ticker_api.html#matplotlib.ticker.LogFormatter

To disable minor tick labeling and use the major ticks as you initially wanted, you can use ax.yaxis.set_minor_formatter(mpl.ticker.NullFormatter()).

If the minor ticks themselves are problematic, then you may wish to instead/in addition change the minor locator ax.yaxis.set_minor_locator(mpl.ticker.NullLocator()) which will get rid of the actual tick mark/minor grid line.

@Spl1x
Copy link
Author

Spl1x commented May 17, 2023

Thank you @ksunden, this makes a lot of sense! I was unaware of this behavior of the LogFormatter.
Closing this issue as this is actually how it should work and I did not properly look into the documentation.

@Spl1x Spl1x closed this as completed May 17, 2023
@QuLogic QuLogic added the Community support Users in need of help. label Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community support Users in need of help.
Projects
None yet
Development

No branches or pull requests

3 participants