Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically offset tick labels in Matplotlib

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?

like image 319
Josh Wood Avatar asked May 12 '26 17:05

Josh Wood


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

like image 171
bnaecker Avatar answered May 14 '26 06:05

bnaecker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!