Centering labels between ticks#

Ticklabels are aligned relative to their associated tick. The alignment 'center', 'left', or 'right' can be controlled using the horizontal alignment property:

However there is no direct way to center the labels between ticks. To fake this behavior, one can place a label on the minor ticks in between the major ticks, and hide the major tick labels and minor ticks.

Here is an example that labels the months, centered between the ticks.

centered ticklabels
import numpy as np
import matplotlib.cbook as cbook
import matplotlib.dates as dates
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt

# Load some financial data; Google's stock price
r = (cbook.get_sample_data('goog.npz', np_load=True)['price_data']
r = r[-250:]  # get the last 250 days

fig, ax = plt.subplots()
ax.plot(, r.adj_close)

# 16 is a slight approximation since months differ in number of days.


# Remove the tick lines
ax.tick_params(axis='x', which='minor', tick1On=False, tick2On=False)

# Align the minor tick label
for label in ax.get_xticklabels(minor=True):
imid = len(r) // 2

Gallery generated by Sphinx-Gallery