【问题标题】:Python Matplotlib: Align text on far left of graphPython Matplotlib:在图表的最左侧对齐文本
【发布时间】: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


【解决方案1】:

替换

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

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

【讨论】:

  • 这不是他需要的
  • 感谢您的回复。这会将文本放置在图形的左侧,而不是图形的最左侧。在水平条形图中,它与条形的边缘而不是 y 轴标签的边缘对齐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 2015-10-16
  • 2022-01-19
  • 1970-01-01
  • 2015-01-25
  • 1970-01-01
  • 2019-11-22
相关资源
最近更新 更多