I was trying to render text with LaTeX in Matplotlib. There is a demo provided by Matplotlib at https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/tex_demo.html.
However, when I ran this demo, I got this error saying "RuntimeError: Failed to process string with tex because latex could not be found", but I'm sure I have installed Miktex and Ghostscript required by Matlibplot.
Anyone knows how should I solve it? Thanks.
import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2
fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)
ax.set_xlabel(r'\textbf{time (s)}')
ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')
plt.show()
Here is the error trace back:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 338, in idle_draw
self.draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
**self._tight_parameters)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
if ax.get_visible()])
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
bb_xaxis = self.xaxis.get_tightbbox(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
self._update_label_position(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
ismath="TeX" if self.get_usetex() else False)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
texfile], tex)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 259, in resize
self.draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
**self._tight_parameters)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
if ax.get_visible()])
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
bb_xaxis = self.xaxis.get_tightbbox(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
self._update_label_position(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
ismath="TeX" if self.get_usetex() else False)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
texfile], tex)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 304, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The System Cannot Find the File Specified
Traceback (most recent call last):
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\tkinter\__init__.py", line 746, in callit
func(*args)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\_backend_tk.py", line 338, in idle_draw
self.draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 1702, in draw
**self._tight_parameters)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
if ax.get_visible()])
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axes\_base.py", line 4355, in get_tightbbox
bb_xaxis = self.xaxis.get_tightbbox(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1164, in get_tightbbox
self._update_label_position(renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 2014, in _update_label_position
bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1999, in _get_tick_boxes_siblings
tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in _get_tick_bboxes
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\axis.py", line 1150, in <listcomp>
for tick in ticks if tick.label1.get_visible()],
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\text.py", line 291, in _get_layout
ismath="TeX" if self.get_usetex() else False)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 448, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 338, in make_dvi
texfile], tex)
File "C:\Users\haiqing\Anaconda3\envs\mie324\lib\site-packages\matplotlib\texmanager.py", line 308, in _run_checked_subprocess
'found'.format(command[0])) from exc
RuntimeError: Failed to process string with tex because latex could not be found
According to the matplotlib
documentation:
Matplotlib's LaTeX support requires a working LaTeX installation, dvipng (which may be included with your LaTeX installation), and Ghostscript (GPL Ghostscript 9.0 or later is required). The executables for these external dependencies must all be located on your PATH.
If you're not familiar with PATH
, this means that the .exe
files of these programs need to be in your system environment variables. In Windows, Edit the System Environment Variables, click on variable Path, and add the filepaths of the aforementioned programs to the list.
LaTex
here
dvipng
here
Ghostscript > 9.0
here
I managed to solve this issue by installing texlive and latex. In apt based Linux, this command installs texlive:
sudo apt install texlive texlive-latex-extra texlive-fonts-recommended dvipng
latex can be easily installed via pip:
pip install latex
When I tried to change the standard font for matplotlib, I likely incorrectly applied a recommended script and broke my matplotlib and from that moment on always got this error message.
Simply resetting matplotlib settings worked perfectly for me.
import matplotlib as mpl
mpl.rcParams.update(mpl.rcParamsDefault)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With