-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Colorbar with imshow(logNorm) shows unexpected minor ticks #8307
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
Comments
Oh, yes, I changed rcParam so that so ticks point inward. Sorry I didn't mention that. First time to report something here. |
Indeed, I would not have expected minor ticks to appear. |
FWIW this issue still present on Matplotlib 2.1. If that was not obvious, please note that the problem does not come from import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import bivariate_normal
from matplotlib.ticker import LogLocator
from matplotlib.colors import LogNorm
N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)
X, Y = np.meshgrid(x, y)
z = (bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0)
+ 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0))
z = np.ma.masked_where(z <= 0, z)
fig, ax = plt.subplots()
cs = ax.imshow(z, norm=LogNorm())
cbar = fig.colorbar(cs)
# print(cbar.ax.yaxis.get_major_locator()) # => <matplotlib.ticker.FixedLocator object at 0x7f0691e57748> : beware, not a LogLocator instance!
cbar.ax.yaxis.set_major_locator(LogLocator()) # <- Why? See above.
cbar.set_ticks(cbar.ax.yaxis.get_major_locator().tick_values(z.min(), z.max()))
fig.show() |
Well, the handling of the tick locator seems a bit convoluted with colorbars, at least for my current understanding ^^... Nevertheless, here is a workaround that is bit simpler than my previous one (it was not obvious to me that one could simply pass a Locator instance to import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import bivariate_normal
from matplotlib.ticker import LogLocator
from matplotlib.colors import LogNorm
N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)
X, Y = np.meshgrid(x, y)
z = (bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0)
+ 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0))
z = np.ma.masked_where(z <= 0, z)
fig, ax = plt.subplots()
cs = ax.imshow(z, norm=LogNorm())
cbar = fig.colorbar(cs)
# One-line workaround (NB: this parameter can also be directly passed to *fig.colorbar*)
cbar.set_ticks(LogLocator()) # defaults are more or less what one wants (otherwise adapt the parameters)
fig.show() |
Ok, if I understood it correctly, the “culprit” may be the following line in locator = ticker.LogLocator(subs='all') that was introduced by 1fe5820. Pinging @efiring, who is apparently our import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator
from matplotlib.colors import LogNorm
# Dummy data
N = 101
x = np.linspace(-3.0, 3.0, N)[np.newaxis, :]
y = np.linspace(-1.0, 1.0, N)[:, np.newaxis]
z = (x.max() - np.abs(x))*(y.max() - np.abs(y))**2 + 1e-3
fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(9.6, 4.8))
for ax, title, ticks in [
(ax0, "Default colorbar\nwith “minor” log ticks", None),
(ax1, "Colorbar with\nonly base log ticks", LogLocator())
]:
cs = ax.imshow(z, norm=LogNorm())
ax.set_title(title)
cbar = fig.colorbar(cs, ax=ax, ticks=ticks)
fig.show() |
ping @efiring who said he was looking at colorbar ticks... |
Yes. Short of a major overhaul of the whole tick system, what is needed is to add full support for minor ticks to the colorbar. In reality this should be needed only for a log scale, but it probably makes more sense to put in all the machinery instead of special-casing log scales. Then colorbar ticking would be more similar to normal axis ticking. It will never be identical, though, because a colorbar simply is not a normal Axis in a normal Axes. |
This is working fine in |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug summary
When using colorbar with imshow(image, norm=LogNorm()), the produced colorbar automatically adds minorticks, and cannot be turned off with cbar.ax.minorticks_off(). Also the font of the tick label is not the same as the font set in matplotlibrc.
Code for reproduction
Actual outcome
Expected outcome
I would expect the colorbar to not have minorticks and to use the same font as other tick labels (I set font to be Arial in matplotlibrc).
Matplotlib version
2.0.0_1, OSX
Macports
Edit by @afvincent: removed the unnecessary instructions coming from the bug report template.
The text was updated successfully, but these errors were encountered: