Vertically offset tick labels in Matplotlib

Viewed 1193

I want tick labels to be placed above the tick instead of centered on the tick.

This is how it looks now: This is how it looks now:

This is how I want it to look: This is how I want it to look:

I've read about tick label formatting here but I could only figure out how modify horizontal padding. I think I need to modify vertical padding. Does anyone have an idea how I could do this?

1 Answers

You can set the vertical alignment of the tick labels directly to shift them up.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.randn(100,)
fig, ax = plt.subplots(2, 1)
ax[0].plot(data)
ax[0].set_title("No shift")
ax[1].plot(data)
for tick in ax[1].get_yticklabels():
    tick.set_verticalalignment("bottom")
ax[1].set_title("With shift")
fig.tight_layout()
plt.show()

You can see that in the first subplot the labels are centered on the ticks, while on the bottom plot they are above it.

enter image description here

Related