【问题标题】:How to centre words in add_artist Text in matplotlib?如何在 matplotlib 的 add_artist 文本中居中单词?
【发布时间】:2021-01-08 04:57:40
【问题描述】:

如何在 matplotlib 的 add_artist Text 中将单词居中?

from matplotlib.text import Text 
import matplotlib.pyplot as plt

fig = plt.figure()

fig.add_artist(Text(0.4, 0.6, text=r"Hello" "\n" r"world!!!!!!!")) 

plt.show()

输出左对齐。

【问题讨论】:

  • 这个方法有什么问题?
  • 我添加了图片。我得到了左对齐的文本。
  • 哦,我现在明白了,感谢您的澄清!

标签: python-3.x matplotlib text


【解决方案1】:

您可以添加参数horizontalalignment='center'。文本布局的文档可以在here找到,如果您需要它以供将来参考。

from matplotlib.text import Text 
import matplotlib.pyplot as plt

fig = plt.figure()

fig.add_artist(Text(0.5, 0.5, text=r"Hello" "\n" r"world!!!!!!!", horizontalalignment='center')) 

plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2016-12-23
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多