I'm having some trouble setting up a pcolormesh plot with a colorbar that includes logarithmically spaced minor tick marks on the colorbar.
The closest I've come is something like this:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
xbins = np.linspace(0, 1, 50)
ybins = np.linspace(0, 1, 50)
data = np.random.random((49,49))
fig, ax = plt.subplots()
im = ax.pcolormesh(xbins, ybins, data, norm=matplotlib.colors.LogNorm())
cb = fig.colorbar(im)
cb.ax.minorticks_on()
plt.savefig('test.png')
The trouble with this solution is that the minor ticks are spaced evenly in log space:
I'd like to set up the plot so I have evenly spaced minor ticks in linear space, which should show up unevenly spaced on this plot.
I know that I can manually set the minor tick labels using a FixedFormatter
, but I'd prefer not to do that if possible since I will be making a large number of plots automatically.
I think the best way to custom colorbars' ticks is to use the "ticks" argument of the fig.colorbar method and not trying to modify the attributes of the axe that contains the colorbar.
from matplotlib.ticker import LogLocator
"..."
cb = fig.colorbar(im, ticks = LogLocator(subs=range(10)))
Added for posterity:
From this answer: @JoeKington https://stackoverflow.com/a/20079644/230468:
minorticks = p.norm(np.arange(1, 10, 2))
cb.ax.xaxis.set_ticks(minorticks, minor=True)
This is annoying that you have to create the tick locations manually, but it seems to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With