Skip to content

Feature request: auto locate minor ticks on log scaled color bar #7141

Closed

Description

@breedlun

Matplotlib version 1.5.1, Python version 2.7.6 (Enthought Canopy) and OSX

As detailed in this stack overflow post, you have to explicitly specify the minor tick locations on a log scaled color bar. It would be nice if matplotlib would do this for you, instead of having to call minorticks = p.norm(np.hstack([np.arange(2, 10, 1)/10.0, np.arange(2, 10, 1)/1.0]) in the code below

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm

x = np.linspace(1,10,10)
y = np.linspace(1,10,10)
X, Y = np.meshgrid(x,y)
Z = np.abs(np.cos(X**2 - Y**2) * X**2 * Y)
f, ax = plt.subplots()
p = plt.pcolormesh(X, Y, Z, norm=LogNorm(), vmin=1e-2, vmax=1e2)
cb = plt.colorbar(p, ax=ax, orientation='horizontal', aspect=10)
# We need to nomalize the tick locations so that they're in the range from 0-1...
minorticks = p.norm(np.hstack([np.arange(2, 10, 1)/10.0, np.arange(2, 10, 1)/1.0]))
cb.ax.xaxis.set_ticks(minorticks, minor=True)
plt.show()

Activity

  1. story645 commented on Sep 20, 2016

    @story645
    Member

    Can you please provide a picture? Might be easier to evaluate what you're after that way.

  2. Kojoley commented on Sep 20, 2016

    @Kojoley
    Member

    Default:
    default

    With minor ticks:
    with minor ticks

  3. Kojoley commented on Sep 20, 2016

    @Kojoley
    Member

    cb.ax.minorticks_on() works in a different way:
    cb.ax.minorticks_on()

  4. breedlun commented on Sep 20, 2016

    @breedlun
    ContributorAuthor

    Thanks, @Kojoley. cb.ax.minorticks_on() gives evenly spaced minor tick marks in log space, which is not what I want. In my opinion, the default should be evenly spaced minor tick marks in linear space, which translate to unevenly spaced minor tick marks in log space.

    The trouble is, I have tried using cb.ax.minorticks_on() and a LogLocator to place the minor tick marks where I want them, but I cannot get them to work together. (The problem might be that the color bar expects tick marks to be normalized from 0 to 1.) The only method that works for me is

    p = plt.pcolormesh(X, Y, Z, norm=LogNorm(), vmin=1e-2, vmax=1e2)
    cb = plt.colorbar(p, ax=ax, orientation='horizontal', aspect=10)
    minorticks = p.norm(np.hstack([np.arange(2, 10, 1)/10.0, np.arange(2, 10, 1)/1.0]))
    cb.ax.xaxis.set_ticks(minorticks, minor=True)
    

    Thus, this seems to be a little more than a simple change to the default settings.

  5. added this to the milestone on Sep 20, 2016
  6. tacaswell commented on Sep 20, 2016

    @tacaswell
    Member
  7. QuLogic commented on Dec 11, 2016

    @QuLogic
    Member

    On current v2.x, this:

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.colors import LogNorm
    
    x = np.linspace(1,10,10)
    y = np.linspace(1,10,10)
    X, Y = np.meshgrid(x,y)
    Z = np.abs(np.cos(X**2 - Y**2) * X**2 * Y)
    f, ax = plt.subplots()
    p = plt.pcolormesh(X, Y, Z, norm=LogNorm(), vmin=1e-2, vmax=1e2)
    cb = plt.colorbar(p, ax=ax, orientation='horizontal', aspect=10)
    plt.show()
    

    looks like this by default:
    figure_1
    so I guess this has been fixed?

  8. efiring commented on Dec 11, 2016

    @efiring
    Member

    Closed by #7438.

  9. breedlun commented on Dec 11, 2016

    @breedlun
    ContributorAuthor

    I am glad to see that minor ticks are placed properly now, but tick length on minor tick should be shorter than major ticks, IMHO. That's how it used to be (see Kojoley's Sep 20th post). Perhaps the minor tick length is just a rcParams setting...

  10. efiring commented on Dec 11, 2016

    @efiring
    Member

    No, it will take more work to fix that. It won't happen for 2.0. It requires substantial change to the colorbar tick-handling, which differs from normal axis tick handling because there is an additional transformation between the colorbar tick values and the axis coordinates. The colorbar does not support major and minor ticks; it has only major ticks. It has a single ticks kwarg and a single format kwarg, together with the machinery to use their values, or suitable defaults based on the norm. Directly setting the minor ticks as in the example at the top of this issue will not work correctly in general because the data limits don't necessarily correspond to the (0, 1) colorbar axis coordinates.
    This is related to the confusing intersection among scales, transforms, norms, tickers, and the necessarily unique characteristics of a colorbar. I would prefer to see it handled in the course of a larger code cleanup and refactoring rather than by merely adding a bunch of code to the already-complex colorbar. (Actually, my mind is wavering; hacking minor ticks into the colorbar is conceptually straightforward, and probably the API changes--adding 2 kwargs--would be the same as we would end up with after a major re-write.)
    I will open a separate issue for this.

  11. breedlun commented on Dec 11, 2016

    @breedlun
    ContributorAuthor

    Ok. Thank you for the detailed reply. I probably would have spent 30 minutes figuring that out on my own.

  12. modified the milestones: , on Dec 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions