Python matplotlib 中 LaTeX 轴标签的粗体字重
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14324477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bold font weight for LaTeX axes label in matplotlib
提问by Hooked
In matplotlibyou can make the text of an axis label bold by
在matplotlib可以使轴标签通过大胆的文字
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$在我的轴标签中加粗?
采纳答案by Andy Hayden
Unfortunately you can't bold symbols using the bold font, see this questionon tex.stackexchange.
不幸的是,您不能使用粗体对符号进行加粗,请参阅tex.stackexchange 上的这个问题。
As the answer suggests, you could use \boldsymbolto bold phi:
正如答案所暗示的那样,您可以使用\boldsymbol粗体 phi:
r'$\boldsymbol{\phi}$'
You'll need to load amsmathinto the TeX preamble:
您需要加载amsmath到 TeX 序言中:
matplotlib.rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
回答by drs
If you intend to have consistently bolded fonts throughout the plot, the best way may be to enable latex and add \boldmathto your preamble:
如果您打算在整个情节中始终使用粗体字体,最好的方法可能是启用 Latex 并添加\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$'}
回答by CJD
As this answer Latex on python: \alpha and \beta don't work?points out. You may have a problem with \bso \boldsymbolmay not work as anticipated. In that case you may use something like: '$ \\\boldsymbol{\\\beta} $'in your python code. Provided you use the preamble plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
作为这个答案Latex on python: \alpha and \beta 不起作用?指出。您可能有问题,\b因此\boldsymbol可能无法按预期工作。在这种情况下,您可以'$ \\\boldsymbol{\\\beta} $'在 python 代码中使用类似的东西。如果您使用序言plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
回答by sodoesaburningbus
In case anyone stumbles across this from Google like I did, another way that doesn't require adjusting the rc preamble (and conflicting with non-latex text) is:
如果有人像我一样从谷歌偶然发现这个,另一种不需要调整 rc 序言(并与非乳胶文本冲突)的方法是:
ax.set_ylabel(r"$\mathbf{\partial y / \partial x}$")

