matplotlib 对齐图表最左侧的文本

rqmkfv5c  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(103)

我试图弄清楚如何在matplotlib中将文本与图形的最左边对齐。我可以通过对值进行硬编码来实现这一点(在下面的示例中,x=-.98),但是这需要大量的尝试和错误。
是否有办法返回图形边框的坐标(而不是图形边框)或将文本设置为从图形的最左边开始?

# matplotlib example of text align
import matplotlib.pyplot as plt

# x and y axis data
y_axis_labels = ['y-label-1','y-label-2','y-label-3','y-label-4']
x_axis_labels = [1,2,3,4]

# create horizontal bar plot
fig, ax = plt.subplots()
ax.barh(y_axis_labels, x_axis_labels)

# Align the text with the far left of the y_axis labels
plt.text(x=-.98,y=4, s='start at far left', color='red')

plt.show()

字符串
Link to example image (I want the red text to align with blue line)

sy5wg1nm

sy5wg1nm1#

更换

plt.text(x=-.98,y=4, s='start at far left', color='red')

字符串

ax.set_title('start at far left',loc='right',color='red' )

相关问题