7
votes

When I try running the very simple code snippet from www.matplotlib.org,

from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)

import numpy as np
import matplotlib.pyplot as plt


# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')
plt.show()

This is highly unusual this code has worked for me before and I cannot figure out what the issue is. The error message that I receive is enormous and shown below. Does anybody know what the issue might be?

Thanks!!

Traceback (most recent call last):

File "/Users/evan/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py", line 332, in call return printer(obj)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 237, in png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))

File "/Users/evan/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 121, in print_figure fig.canvas.print_figure(bytes_io, **kw)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2216, in print_figure **kwargs)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 507, in print_png FigureCanvasAgg.draw(self)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 430, in draw self.figure.draw(self.renderer)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper return draw(artist, renderer, *args, **kwargs)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 1299, in draw renderer, self, artists, self.suppressComposite)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images a.draw(renderer)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper return draw(artist, renderer, *args, **kwargs)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2437, in draw mimage._draw_list_compositing_images(renderer, self, artists)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images a.draw(renderer)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper return draw(artist, renderer, *args, **kwargs)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py", line 1135, in draw renderer)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py", line 1075, in _get_tick_bboxes extent = tick.label1.get_window_extent(renderer)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/text.py", line 933, in get_window_extent bbox, info, descent = self._get_layout(self._renderer)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/text.py", line 308, in _get_layout ismath=False)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 226, in get_text_width_height_descent s, fontsize, renderer=self)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 602, in get_text_width_height_descent dvifile = self.make_dvi(tex, fontsize)

File "/Users/evan/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 393, in make_dvi stderr=subprocess.STDOUT)

File "/Users/evan/anaconda3/lib/python3.6/subprocess.py", line 336, in check_output **kwargs).stdout

File "/Users/evan/anaconda3/lib/python3.6/subprocess.py", line 403, in run with Popen(*popenargs, **kwargs) as process:

File "/Users/evan/anaconda3/lib/python3.6/subprocess.py", line 709, in init restore_signals, start_new_session)

File "/Users/evan/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename)

FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex'

2
What did you change in between? The error suggests you do not have latex or that latex cannot be found. So maybe it is not in your path any more?ImportanceOfBeingErnest
I really don't know, I cannot find the issue..Evan
The comment was meant for you to check if latex is in your path.ImportanceOfBeingErnest
Did adding the path to the LaTeX binary solve this?tallamjr
No - this was several months ago but if I am not mistaken I just had to comment the line of code referencing TeX out.Evan

2 Answers

1
votes

For linux:

 sudo yum install latex
 sudo yum install dvipng

For Window: install texlive.

ref. https://matplotlib.org/tutorials/text/usetex.html?highlight=latex

0
votes

Download miktex and add the path in environment variables for Windows.