5

I am trying to customize the minor ticks in a matplotlib plot. Consider the following code:

import pylab as pl
from matplotlib.ticker import AutoMinorLocator

fig, ax = pl.subplots(figsize=(11., 7.4))

x = [1,2,3, 4]
y = [10, 45, 77, 55]
errorb = [20,66,58,11]

pl.xscale("log")

ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

pl.tick_params(which='both', width=1)
pl.tick_params(which='minor', length=4, color='g')
pl.tick_params(axis ='both', which='major', length=8, labelsize =20, color='r' )

pl.errorbar(x, y, yerr=errorb)
#pl.plot(x, y)

pl.show()

As far as I understood, AutoMinorLocator(n) is supposed to insert n minor ticks between each major tick, and this is what happens on a linear scale but simply cannot figure out the logic behind the placement of the minor ticks on a logscale. On the top of that, there are much more minor ticks when using errorbar() then when using the simple plot().

Botond
  • 2,640
  • 6
  • 28
  • 44

1 Answers1

3

AutoMinorLocator is only designed to work for linear scales:

From the ticker documentation:

AutoMinorLocator

locator for minor ticks when the axis is linear and the major ticks are uniformly spaced. It subdivides the major tick interval into a specified number of minor intervals, defaulting to 4 or 5 depending on the major interval.

And the AutoMinorLocator documentation:

Dynamically find minor tick positions based on the positions of major ticks. Assumes the scale is linear and major ticks are evenly spaced.

You probably want to use the LogLocator for your purposes.

For example, to put major ticks in base 10, and minor ticks at 2 and 5 on your plot (or every base*i*[2,5]), you could:

ax.xaxis.set_major_locator(LogLocator(base=10))
ax.xaxis.set_minor_locator(LogLocator(base=10,subs=[2.0,5.0]))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165