简体   繁体   English

matplotlib 中 LaTeX 轴标签的粗体字重

[英]Bold font weight for LaTeX axes label in matplotlib

In matplotlib you can make the text of an axis label bold bymatplotlib中,您可以通过以下方式使轴标签的文本变为粗体

plt.xlabel('foo',fontweight='bold')

You can also use LaTeX with the right backend您还可以将 LaTeX 与正确的后端一起使用

plt.xlabel(r'$\phi$')

When you combine them however, the math text is not bold anymore但是,当您组合它们时,数学文本不再是粗体

plt.xlabel(r'$\phi$',fontweight='bold')

Nor do the following LaTeX commands seem to have any effect以下 LaTeX 命令似乎也没有任何效果

plt.xlabel(r'$\bf \phi$')
plt.xlabel(r'$\mathbf{\phi}$')

How can I make a bold $\phi$ in my axis label?如何在我的轴标签中制作粗体$\phi$

Unfortunately you can't bold symbols using the bold font, see this question on tex.stackexchange.不幸的是,您不能使用粗体字体加粗符号,请参阅 tex.stackexchange 上的这个问题

As the answer suggests, you could use \boldsymbol to bold phi:正如答案所暗示的,您可以使用\boldsymbol来加粗 phi:

r'$\boldsymbol{\phi}$'

You'll need to load amsmath into the TeX preamble:您需要将amsmath加载到 TeX 序言中:

matplotlib.rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]

If you intend to have consistently bolded fonts throughout the plot, the best way may be to enable latex and add \boldmath to your preamble:如果您打算在整个情节中始终使用粗体字体,最好的方法可能是启用乳胶并将\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']

Then your axis or figure labels can have any mathematical latex expression and still be bold:然后你的轴或图形标签可以有任何数学乳胶表达式,并且仍然是粗体:

plt.xlabel(r'$\frac{\phi + x}{2}$')

However, for portions of labels that are not mathematical, you'll need to explicitly set them as bold:但是,对于不是数学的标签部分,您需要将它们显式设置为粗体:

plt.ylabel(r'\textbf{Counts of} $\lambda$'}

如果有人像我一样从谷歌偶然发现这个,另一种不需要调整 rc 序言(并且与非乳胶文本冲突)的方法是:

ax.set_ylabel(r"$\mathbf{\partial y / \partial x}$")

当使用 LaTeX 排版图形中的所有文本时,您可以使用\textbf使“正常”(非方程式)文本变为粗体:

ax.set_title(r"\textbf{some text}")

None of these solutions worked for me and I was astonished to find something so simple was so infuriating to achieve.这些解决方案都不适合我,我惊讶地发现如此简单的事情却如此令人愤怒。 In the end, this is what worked for my use case.最后,这对我的用例有用。 I would advise adapting this for your own use:我建议调整它以供您自己使用:

plt.suptitle(r"$ARMA({0}, {1})$ Multi-Parameter, $\bf{{a}}$, Electrode Response".format(n_i, m), fontsize=16)

The {0} and {1} refer to positional arguments supplied to format method, meaning 0 refers to variable n_i and 1 refers to variable m . {0}{1}指的是提供给format方法的位置参数,这意味着0指的是变量n_i1指的是变量m

Note: In my setup, for some reason, \textbf did not work.注意:在我的设置中,由于某种原因, \textbf不起作用。 I have read somewhere that \bf is deprecated in LaTeX, but for me this is what worked.我在某处读到\bf在 LaTeX 中已被弃用,但对我来说这是有效的。

As this answer Latex on python: \alpha and \beta don't work?作为这个答案Latex on python: \alpha 和 \beta 不起作用? points out.指出。 You may have a problem with \b so \boldsymbol may not work as anticipated.您可能对\b有疑问,因此\boldsymbol可能无法按预期工作。 In that case you may use something like: '$ \\\boldsymbol{\\\beta} $' in your python code.在这种情况下,您可以在 python 代码中使用类似: '$ \\\boldsymbol{\\\beta} $' Provided you use the preamble plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]如果您使用前导plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]

I wanted to do something similar only then plot 'K' with subscript '1/2' as label and in bold.我想做类似的事情,然后用下标'1/2'作为标签并以粗体绘制'K'。 This worked without changing any of the rc parameters.这在不更改任何 rc 参数的情况下工作。

plt.figure()
plt.xlabel(r'$\bf{K_{1/2}}$')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM