Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logarithmically scaled minor tick marks on a matplotlib colorbar?

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:

enter image description here

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.

like image 312
ngoldbaum Avatar asked Aug 22 '14 19:08

ngoldbaum


2 Answers

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

enter image description here

like image 179
user2660966 Avatar answered Sep 30 '22 10:09

user2660966


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.

like image 30
DilithiumMatrix Avatar answered Sep 30 '22 09:09

DilithiumMatrix