简体   繁体   中英

Bold font weight for LaTeX axes label in matplotlib

In matplotlib you can make the text of an axis label bold by

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

You can also use LaTeX with the right backend

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

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

How can I make a bold $\phi$ in my axis label?

Unfortunately you can't bold symbols using the bold font, see this question on tex.stackexchange.

As the answer suggests, you could use \boldsymbol to bold phi:

r'$\boldsymbol{\phi}$'

You'll need to load amsmath into the TeX preamble:

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:

# 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 .

Note: In my setup, for some reason, \textbf did not work. I have read somewhere that \bf is deprecated in LaTeX, but for me this is what worked.

As this answer Latex on python: \alpha and \beta don't work? points out. You may have a problem with \b so \boldsymbol may 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}"]

I wanted to do something similar only then plot 'K' with subscript '1/2' as label and in bold. This worked without changing any of the rc parameters.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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