45

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?

Hooked
  • 84,485
  • 43
  • 192
  • 261

9 Answers9

41

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}"]
Community
  • 1
  • 1
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • This does not work: `ValueError: \boldsymbol{\phi} ^ Unknown symbol: \boldsymbol (at char 0), (line:1, col:1) ` perhaps it requires that we need `amsmath` loaded? Have you tested this on your machine? – Hooked Jan 14 '13 at 19:15
  • 1
    @Hooked I think including a preamble should work as described [here](http://www.inductiveload.com/posts/multi-line-latex-expressions-in-matplotlib/): `matplotlib.rc('text', usetex=True)`, `matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]`. Unfortunately I can't test yet, will update when I have. – Andy Hayden Jan 14 '13 at 19:52
  • 1
    On Matpotlib 3.3.3, with Python 3 7.13.0, adding those commands to rcParams leads to the following error, when trying to plot: `! LaTeX Error: File type1ec.sty not found.` – MRule Nov 23 '20 at 11:51
  • 1
    @MRule - See https://github.com/matplotlib/matplotlib/issues/16911 - On Debian and derivatives: `sudo apt install cm-super` - on macOS: `sudo tlmgr install cm-super` - on other systems: see packages listed at https://repology.org/projects/?search=cmsuper or https://repology.org/projects/?search=cm-super or https://repology.org/projects/?search=fontsrecommended – Samuel Lelièvre Dec 01 '21 at 12:52
  • 3
    `matplotlib.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]` did not work for me (Python 3.9.9), but `matplotlib.rc('text.latex', preamble=r'\usepackage{amsmath}')` did! Thanks to [this post](https://stackoverflow.com/questions/41453109/how-to-write-your-own-latex-preamble-in-matplotlib#41453758) – Maze Dec 06 '21 at 04:03
  • In addition to `apt-get install cm-super`, I also had to `apt-get install dvipng`. With no additional changes, this provides the `\bf` symbol but not `\mathbf` or `\boldsymbol`. Adding then `rc('text', usetex=True); rc('text.latex',preamble=r"\usepackage{amsmath}")` makes both `\mathbf` and `\boldsymbol` available! It works! – MRule Jan 26 '22 at 13:46
19

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$'}
drs
  • 5,679
  • 4
  • 42
  • 67
18

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:

ax.set_ylabel(r"$\mathbf{\partial y / \partial x}$")
xiawi
  • 1,772
  • 4
  • 19
  • 21
  • Having just trawled through a tonne of info on path variables and ways to configure rcParams etc. this was the best solution for me, if nothing else because it doesn't rely on anything else and achieves exactly what is needed. As a heads up to anyone reading this also wanting to use \mathit{}, you can add spaces between words or characters using a double backslash:) – whatf0xx Jan 12 '23 at 14:09
2

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}}$')
JonnDough
  • 827
  • 6
  • 25
1

When using LaTeX to typeset all text in a figure, you can make "normal" (non-equation) text bold by using \textbf:

ax.set_title(r"\textbf{some text}")
Blub Bla
  • 136
  • 8
1

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.

Sameen
  • 729
  • 6
  • 19
1

The accepted answer seems to include deprecated parameter setting syntax. I had the same issues found in the comments to that answer.

The following worked for me:

plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{amsmath}')

Found here:

How does one use LaTeX/amsmath with matplotlib?

Hooked
  • 84,485
  • 43
  • 192
  • 261
0

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}"]

Ahmad
  • 227
  • 3
  • 15
CJD
  • 171
  • 1
  • 1
  • 13
  • Thank you for this input (on a 4 year old question!), but using `r` in front of a string should help escape the `\b`. The answer provided by @AndyHayden works fine and identifies the problem. – Hooked Jun 13 '17 at 21:40
0

Update for recent Matplotlib versions

In more recent versions of Matplotlib, the preamble must be specified as a string.

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}$")

This uses the default LaTeX font "Computer Modern" for a more natural look.

Instead of \bm, may can alternatively use the older \boldsymbol from \usepackage{amsmath}.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135