matplotlib
中,您可以通过 将轴标签的文本设为粗体
plt.xlabel('foo',fontweight='bold')
您还可以在正确的后端使用 LaTeX
plt.xlabel(r'$\phi$')
但是,当您将它们组合起来时,数学文本不再是粗体了
plt.xlabel(r'$\phi$',fontweight='bold')
以下 LaTeX 命令似乎也没有任何效果
plt.xlabel(r'$\bf \phi$')
plt.xlabel(r'$\mathbf{\phi}$')
如何在轴标签中加粗
$\phi$
?
不幸的是,您无法使用粗体字体将符号加粗,请参阅 tex.stackexchange 上的这个问题。
正如答案所示,您可以使用
\boldsymbol
来粗体 phi:
r'$\boldsymbol{\phi}$'
您需要将
amsmath
加载到 TeX 序言中:
matplotlib.rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
如果您打算在整个情节中使用一致的粗体字体,最好的方法可能是启用乳胶并将
\boldmath
添加到您的序言中:
# Optionally set font to Computer Modern to avoid common missing font errors
matplotlib.rc('font', family='serif', serif='cm10')
matplotlib.rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']
然后你的轴或图形标签可以有任何数学乳胶表达式并且仍然是粗体:
plt.xlabel(r'$\frac{\phi + x}{2}$')
但是,对于非数学标签部分,您需要将它们显式设置为粗体:
plt.ylabel(r'\textbf{Counts of} $\lambda$'}
万一有人像我一样从 Google 偶然发现这个问题,另一种不需要调整 rc 前导码(并且与非乳胶文本冲突)的方法是:
ax.set_ylabel(r"$\mathbf{\partial y / \partial x}$")
我想做类似的事情,然后用下标“1/2”作为标签并以粗体绘制“K”。这无需更改任何 rc 参数即可工作。
plt.figure()
plt.xlabel(r'$\bf{K_{1/2}}$')
当使用 LaTeX 排版图中的所有文本时,您可以使用
\textbf
: 将“普通”(非方程式)文本设为粗体
ax.set_title(r"\textbf{some text}")
这些解决方案都不适合我,我很惊讶地发现如此简单的事情实现起来如此令人恼火。最后,这对我的用例有用。我建议将其改编为您自己使用:
plt.suptitle(r"$ARMA({0}, {1})$ Multi-Parameter, $\bf{{a}}$, Electrode Response".format(n_i, m), fontsize=16)
{0}
和{1}
指的是提供给format
方法的位置参数,这意味着0
指的是变量n_i
,而1
指的是变量m
。
注意:在我的设置中,由于某种原因,
\textbf
不起作用。我在某处读到 \bf
在 LaTeX 中已被弃用,但对我来说这是有效的。
接受的答案似乎包括已弃用的参数设置语法。我在该答案的评论中发现了同样的问题。
以下对我有用:
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{amsmath}')
在这里找到:
正如这个答案 Latex on python: lpha and eta don't work? 指出的那样。您可能对
\b
有疑问,因此 \boldsymbol
可能无法按预期工作。在这种情况下,您可以在 Python 代码中使用类似:'$ \\\boldsymbol{\\\beta} $'
的内容。前提是您使用序言 plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
在 Matplotlib 的最新版本中,必须将前导码指定为字符串。
import matplotlib.pyplot as plt
plt.rcParams.update(
{
"text.usetex": True,
"text.latex.preamble": r"\usepackage{bm}",
# Enforce default LaTeX font.
"font.family": "serif",
"font.serif": ["Computer Modern"],
}
)
# ...
plt.xlabel(r"$\bm{\phi}$")
这使用 默认 LaTeX 字体
"Computer Modern"
以获得更自然的外观。
可以使用
\bm
中的旧版 \boldsymbol
代替 \usepackage{amsmath}
。