I'm trying to create some labels manually which should align exactly with the tick locations. However, when plotting text ha='center'
aligns the bounding box of the text in the center, but the text itself within the bounding box is shifted to the left.
How can I align the text itself in the center? I found this question but it doesn't help as it shifts the bounding box, while I need to shift the text.
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
print(matplotlib.__version__) # 3.5.3
fig, ax = plt.subplots()
ax.plot([.5, .5],
[0, 1],
transform=ax.transAxes)
ax.text(.5,
.5,
'This text needs to be center-aligned'.upper(),
ha='center',
va='center',
rotation='vertical',
transform=ax.transAxes,
bbox=dict(fc='blue', alpha=.5))
ax.set_title('The box is center-aligned but the text is too much to the left')
plt.show()