【发布时间】:2020-12-20 02:36:46
【问题描述】:
我正在尝试弄清楚如何将文本与 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)
谢谢!
【问题讨论】:
-
试试这个
plt.title('haha', x=-0.14049, y=0.98)?
标签: python matplotlib text alignment