Skip to content

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

Closed

Description

@doronbehar

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.

Activity

  1. anntzer commented on Jan 6, 2025

    @anntzer
    Contributor

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

  2. doronbehar commented on Jan 7, 2025

    @doronbehar
    ContributorAuthor

    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

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