python - matplotlib: draw major tick labels under minor labels -
this seems should easy - can't see how it:
i have plot time on x-axis. want set 2 sets of ticks, minor ticks showing hour of day , major ticks showing day/month. this:
# set date ticks sensible: xax = ax.get_xaxis() xax.set_major_locator(dates.daylocator()) xax.set_major_formatter(dates.dateformatter('%d/%b')) xax.set_minor_locator(dates.hourlocator(byhour=range(0,24,3))) xax.set_minor_formatter(dates.dateformatter('%h')) this labels ticks ok, major tick labels (day/month) drawn on top of minor tick labels:

how force major tick labels plotted below minor ones? tried putting newline escape characters (\n) in dateformatter, poor solution vertical spacing not quite right.
any advice appreciated!
you can use axis method set_tick_params() keyword pad. compare following example.
import datetime import random import matplotlib.pyplot plt import matplotlib.dates dates # make data x = [datetime.datetime.now() + datetime.timedelta(hours=i) in range(100)] y = [i+random.gauss(0,1) i,_ in enumerate(x)] # plot plt.plot(x,y) # beautify x-labels plt.gcf().autofmt_xdate() ax = plt.gca() # set date ticks sensible: xax = ax.get_xaxis() xax.set_major_locator(dates.daylocator()) xax.set_major_formatter(dates.dateformatter('%d/%b')) xax.set_minor_locator(dates.hourlocator(byhour=range(0,24,3))) xax.set_minor_formatter(dates.dateformatter('%h')) xax.set_tick_params(which='major', pad=15) plt.show() ps: example borrowed moooeeeep
Comments
Post a Comment