Osmond Osmond - 3 months ago 17
Python Question

Centering x-tick labels between tick marks in matplotlib

I want to have the x-tick date labels centered between the tick marks, instead of centered about the tick marks as shown in the photo below.

I have read the documentation but to no avail - does anyone know a way to do this?

enter image description here

Here is everything that I've used for my x-axis tick formatting if it helps:

day_fmt = '%d'
myFmt = mdates.DateFormatter(day_fmt)

for tick in ax.xaxis.get_major_ticks():


One way to do it is to use the minor ticks. The idea is that you set the minor ticks so that they are located halfway between the major ticks, and you manually specify the labels.

For example:

import matplotlib.ticker as ticker

# a is an axes object, from figure.get_axes()

# Hide major tick labels

# Customize minor tick labels

The three lines:

  • "Hide" the 1,2,3,4,... that you have on the major ticks
  • Set minor ticks halfway between the major ticks (assuming your major ticks are at 1,2,3...)
  • Manually specifies the labels for the minor ticks. Here, '1' would be between 1.0 and 2.0 on the graph.

This is just a simple example. You would probably want to streamline it a bit by populating the lists in a loop or something.

You can also experiment with other locators or formatters.

EDIT : Examples

Before: Before

After: enter image description here