Alternative tick labels in matplotlib
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')
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 to share