我正在尝试手动创建一些标签,这些标签应该与刻度位置完全对齐。然而,当绘制文本时,
我应该如何将文本本身居中对齐?我找到了this question,但它没有帮助,因为它只能移动边界框,而我需要移动文本本身。
ha='center'
会将文本的边界框居中对齐,但是文本本身在边界框内部被向左移动。我应该如何将文本本身居中对齐?我找到了this question,但它没有帮助,因为它只能移动边界框,而我需要移动文本本身。
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()
y
、g
、q
、p
、ç
等)。 - JohanC