-2

I'm using a log-log plot with matplotlib.pyplot with an x-axis that only varies over a few orders of magnitude. With only the major tics labeled, the x-axis looks very sparse and a little unclear. How can I label the minor tics as well?

Dan
  • 12,157
  • 12
  • 50
  • 84

2 Answers2

2

What do you want to put on the minor tick labels? If you just want to make your axis look a little more lively, you could

import numpy as np
from matplotlib.ticker import LogLocator, FormatStrFormatter

# display 5 minor ticks between each major tick
minorLocator = LogLocator(subs=np.linspace(2,10,6,endpoint=False))
# format the labels (if they're the x values)
majorFormatter = FormatStrFormatter('%5.4f')

# for no labels use default NullFormatter
ax.xaxis.set_minor_locator(minorLocator)

# or if you want to see some constrained floats 
# this may make things busy!
ax.xaxis.set_minor_formatter(minorFormatter)
danodonovan
  • 19,636
  • 10
  • 70
  • 78
  • 1
    [`MultipleLocator`](http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.MultipleLocator) is a locator for each multiple of its argument, so in this case the ticks would be 0,5,10,15,20,25,..., i.e. not five minor ticks between each major tick. – sodd May 30 '13 at 14:32
  • 1
    This doesn't seem to give the correct result either. You have given the parameter `subs` the argument `[1.0]`, which means that the minor ticks will be displayed only for 10**1 * 1, 10**2 * 1, 10**3 * 1 and so on. The `numticks` parameter tries to fit approximately as many tickmarks on the axis as its argument (at least that seems to be the case), in this case 5. Both these arguments prevent the desired output. You should simply call `LogLocator(subs=np.linspace(2,10,6,endpoint=False))` to get five minor tickmarks between every major tickmark. – sodd May 30 '13 at 17:17
1

I would do something like:

plt.xticks([1, 3, 10, 30, 100, 300], [1, 3, 10, 30, 100, 300])

You should probably put this in a convenience function that gets the current axis limits and generates the appropriate sequence of ticks and tick labels.

There might be a "Ticker" object in matplotlib that does something like this. The documentation is pretty sparse on log plots, unfortunately.

cxrodgers
  • 4,317
  • 2
  • 23
  • 29