4

I'm looking for a way to alternate my x-axis tick labels across two levels. My labels now are:

A B C D

What I want would be

A   C
  B   D

But I can't seem to find how on google/docs/SO.

Thanks a lot!

Kevin
  • 775
  • 2
  • 13
  • 32

1 Answers1

3

You can use minor ticks (defined via set_minor_locator and set_minor_formatter). Then you can shift e.g. all major ticks using tick_params:

import pylab as pl
from matplotlib.ticker import FixedLocator, FormatStrFormatter

pl.plot(range(100))

pl.axes().xaxis.set_major_locator(FixedLocator(range(0, 100, 10)))
pl.axes().xaxis.set_minor_locator(FixedLocator(range(5, 100, 10)))
pl.axes().xaxis.set_minor_formatter(FormatStrFormatter("%d"))
pl.axes().tick_params(which='major', pad=20, axis='x')

enter image description here

But be careful with FixedLocator: To avoid generating major ticks twice (once as major and once as minor ticks) I chose fixed tick locations instead of using a MultipleLocator. You'll need to adjust these tick locations to your data and window size.

(Idea borrowed from here.)

Community
  • 1
  • 1
Falko
  • 17,076
  • 13
  • 60
  • 105
  • This did the trick. Was hoping that I could just order major-ticks in the way requested, but this worked :). – Kevin Aug 21 '14 at 20:32