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

Skip to content

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

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
breedlun opened this issue Sep 20, 2016 · 10 comments
Closed

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

breedlun opened this issue Sep 20, 2016 · 10 comments

Comments

@breedlun
Copy link
Contributor

breedlun commented Sep 20, 2016

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()
@story645
Copy link
Member

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

@Kojoley
Copy link
Member

Kojoley commented Sep 20, 2016

Default:
default

With minor ticks:
with minor ticks

@Kojoley
Copy link
Member

Kojoley commented Sep 20, 2016

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

@breedlun
Copy link
Contributor Author

breedlun commented Sep 20, 2016

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.

@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Sep 20, 2016
@tacaswell
Copy link
Member

attn @efiring

@QuLogic
Copy link
Member

QuLogic commented Dec 11, 2016

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?

@efiring
Copy link
Member

efiring commented Dec 11, 2016

Closed by #7438.

@efiring efiring closed this as completed Dec 11, 2016
@breedlun
Copy link
Contributor Author

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...

@efiring
Copy link
Member

efiring commented Dec 11, 2016

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.

@breedlun
Copy link
Contributor Author

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

@QuLogic QuLogic modified the milestones: 2.0 (style change major release), 2.0.1 (next bug fix release) Dec 12, 2016
breedlun pushed a commit to breedlun/clearplot that referenced this issue Oct 21, 2020
…atplotlib 1.5.1, but no longer appear to be needed. (For example, matplotlib/matplotlib#7141 is now resolved.)
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

6 participants