Skip to content

[ENH]: Compute ticks of log scaled axes a bit better? #29414

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
doronbehar opened this issue Jan 6, 2025 · 2 comments
Closed

[ENH]: Compute ticks of log scaled axes a bit better? #29414

doronbehar opened this issue Jan 6, 2025 · 2 comments

Comments

@doronbehar
Copy link
Contributor

Problem

Taken from here, I found the following ticks (from plt.semilogy([1.5, 50])):

image

Not informative enough. I wish there was an easy way to set_minor_formatter such that say e.g 2x10^0, 3x10^0 would have been displayed. I tried to use (full MWE this time):

import matplotlib.pyplot as plt
from matplotlib import ticker

plt.semilogy([1.5, 50])
plt.gca().yaxis.set_minor_formatter(ticker.LogFormatter(base=10, labelOnlyBase=True))
plt.show()

But it changed nothing. OTH I was capable to obtain my desired output with:

#!/usr/bin/env python

import numpy as np

import matplotlib.pyplot as plt
from matplotlib import ticker

plt.semilogy([1.5, 50])
def get_minor_tick_string(x, pos):
    b = np.floor(np.log10(x)).astype(int)
    p = pos % 8
    if p < 3:
        return f"${p+2}\\cdot 10^{b}$"
    else:
        return ""
plt.gca().yaxis.set_minor_formatter(get_minor_tick_string)
plt.show()

But I'm sure it is not very versatile. It produces:

image

Which is not bad.

Proposed solution

I'd like to first explain in the docs / fix the behavior of set_minor_formatter(ticker.LogFormatter(base=10, labelOnlyBase=True)). 2ndly, I'd like to discuss the possibility of adding tick labels as shown above, because I think I'm not the only one that was not satisfied by this default behavior.

@anntzer
Copy link
Contributor

anntzer commented Jan 6, 2025

This may have been improved by #29054 (which will be in mpl3.11)?

@doronbehar
Copy link
Contributor Author

This may have been improved by #29054 (which will be in mpl3.11)?

This is perfect! Thanks for the quick reply and for the link.

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