• 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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}"]
  • 27
Reply Report
      • 2
    • This does not work: ValueError: oldsymbol{phi} ^ Unknown symbol: oldsymbol (at char 0), (line:1, col:1) perhaps it requires that we need amsmath loaded? Have you tested this on your machine?
      • 2
    • @Hooked I think including a preamble should work as described here: matplotlib.rc('text', usetex=True), matplotlib.rcParams['text.latex.preamble']=[r"usepackage{amsmath}"]. Unfortunately I can't test yet, will update when I have.

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$'}
  • 17
Reply Report

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

  • 0
Reply Report
      • 2
    • Thank you for this input (on a 4 year old question!), but using r in front of a string should help escape the . The answer provided by @AndyHayden works fine and identifies the problem.

Trending Tags