Alternative tick labels in matplotlib

I'm looking for a way to alternate the x-axis label labels in two levels. Now my shortcuts are:

ABCD

What I want will be

A   C
  B   D

      

But I cannot find how on google / docs / SO.

Thank you so much!

+3


source to share


1 answer


You can use small ticks (defined with set_minor_locator

and set_minor_formatter

). Then you can move eg. 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 MultipleLocator

. You need to adjust these tick locations to your data and window size.

(Idea borrowed from here .)

+2


source







All Articles