user1988816
user1988816

Reputation: 11

IPython Notebook: Plotting with LaTeX?

Displaying lines of LaTeX in IPython Notebook has been answered previously, but how do you, for example, label the axis of a plot with a LaTeX string when plotting in IPython Notebook?

Upvotes: 1

Views: 13747

Answers (3)

Jacob Helwig
Jacob Helwig

Reputation: 61

I was able to use matplotlib with TeX in python scripts and in the python interpreter, but in notebooks, I got the following error:

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
preamble = r'''
\renewcommand*\familydefault{\sfdefault} 
\usepackage{sfmath}
\usepackage{amsmath}
'''
plt.rc('text.latex', preamble=preamble)
plt.plot([1,2,3])
plt.title(r"$f_{\text{cor, r}}$")
plt.show()

FileNotFoundError: Matplotlib's TeX implementation searched for a file named 'cmss10.tfm' in your texmf tree, but could not find it

The following solution worked for me on Linux and is based on https://stackoverflow.com/a/71375148/10965084.

First, I installed TeX manually following this tutorial: https://gist.github.com/chiang-yuan/62fbcaae06bf77f793a8f9b5aed1ba70. I then added the path to TeX Live to my .bashrc: export PATH=/path/to/texlive/bin/x86_64-linux:$PATH.

The root cause of the problem was revealed when I ran the following in python3.10:

import subprocess
result = subprocess.run(['which', 'tex'], stdout=subprocess.PIPE)
result.stdout

In the python interpreter, this gave /path/to/texlive/bin/x86_64-linux, but in the jupyter notebook, it gave an empty string.

The fix was then to add the following to the notebook:

PATH = "/path/to/texlive/bin/x86_64-linux"
import os
os.environ["PATH"] += ":" + PATH

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
preamble = r'''
\renewcommand*\familydefault{\sfdefault} 
\usepackage{sfmath}
\usepackage{amsmath}
'''
plt.rc('text.latex', preamble=preamble)
plt.plot([1,2,3])
plt.title(r"$f_{\text{cor, r}}$")
plt.show()

Upvotes: 0

spring
spring

Reputation: 291

I ran into the problem posted in the comments: ! LaTeX Error: File 'type1cm.sty' not found.

The issue was that my default tex command was not pointing to my up-to-date MacTex distribution, but rather to an old distribution of tex which I had installed using macports a few years back and which wasn't being updated since I had switched to using MacTex.

I diagnosed this by typing which tex on the command line and getting /opt/local/bin/tex which is not the default install location for MacTex.

The solution was that I had to edit my $PATH variable so that the right version of tex would get called by matplotlib.

I added export PATH="/usr/local/texlive/2019/bin/x86_64-darwin:$PATH" on the last line of my ~/.bash_profile.

Now when I write echo $PATH on the command line I get:

/usr/local/texlive/2019/bin/x86_64-darwin:blah:blah:blah...

Don't forget to restart both your terminal and your jupyter server afterwards for the changes to take effect.

Upvotes: 2

unutbu
unutbu

Reputation: 880399

It works the same in IPython as it does in a stand-alone script. This example comes from the docs:

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('text', usetex = True)
mpl.rc('font', family = 'serif')
plt.figure(1, figsize = (6, 4))
ax = plt.axes([0.1, 0.1, 0.8, 0.7])
t = np.arange(0.0, 1.0+0.01, 0.01)
s = cos(2*2*pi*t)+2
plt.plot(t, s)

plt.xlabel(r'\textbf{time (s)}')
plt.ylabel(r'\textit{voltage (mV)}', fontsize = 16)
plt.title(r"\TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
      fontsize = 16, color = 'r')
plt.grid(True)
plt.savefig('tex_demo')
plt.show()

enter image description here

Upvotes: 9

Related Questions