matplotlib 图形的乳胶渲染文本中的中心标题

2024-05-17

我想将 Matplotlib 图形的标题居中,其中在渲染 LaTeX 样式时包含换行符返回 (\)

在标题中间插入 Latex \\ 的简单返回代码可以工作,但不会使其居中,从而导致换行符从第一行尴尬地移动。

from matplotlib import pyplot as plt
plt.rc('text', usetex=True)
plt.title(r"\center{\textbf{Some title \\ with with a newline}}")

or

plt.title(r"\begin{centering}{\textbf{Some title \\ with a newline}\end{centering}")

行不通

当我输入前面引用的行时,我在图中根本没有得到任何标题输出。不过,我在 python 命令解释器上没有出现 LaTeX 错误。


您可以轻松使用\n获取换行符plt.title():

import numpy as np
import matplotlib.pyplot as plt

plt.rc('text', usetex=True)

t = np.arange(0., 5., 0.2)

plt.plot(t, t/5000,)
plt.title(r"$\textbf{Some title}$"
          "\n"
          r"$\textbf{with a formula}:$"
          "\n"
          r"$y=t/5000$")
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

matplotlib 图形的乳胶渲染文本中的中心标题 的相关文章