Repository navigation
Closed
Labels
Description
Bug report
Bug summary
The sample script given in
https://matplotlib.org/gallery/text_labels_and_annotations/tex_demo.html?highlight=tex
does not work, producing a large number of error messages and an empty (blank) plotting window.
Code for reproduction
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()Actual outcome
F:\Documents\Programs\Python>python TestTeX2.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\_backend_tk.py", line 227, in resize
self.draw()
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 12, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_agg.py", line 437, in draw
self.figure.draw(self.renderer)
File "C:\Program Files\Python\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files\Python\lib\site-packages\matplotlib\figure.py", line 1484, in draw
**self._tight_parameters)
File "C:\Program Files\Python\lib\site-packages\matplotlib\figure.py", line 2307, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Program Files\Python\lib\site-packages\matplotlib\tight_layout.py", line 349, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Program Files\Python\lib\site-packages\matplotlib\tight_layout.py", line 115, in auto_adjust_subplotpars
if ax.get_visible()])
File "C:\Program Files\Python\lib\site-packages\matplotlib\axes\_base.py", line 4192, in get_tightbbox
bb.append(self.title.get_window_extent(renderer))
File "C:\Program Files\Python\lib\site-packages\matplotlib\text.py", line 924, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Program Files\Python\lib\site-packages\matplotlib\text.py", line 309, in _get_layout
ismath=ismath)
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_agg.py", line 236, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 501, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 365, in make_dvi
texfile], tex)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 335, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Program Files\Python\lib\subprocess.py", line 216, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Program Files\Python\lib\subprocess.py", line 394, in __init__
errread, errwrite)
File "C:\Program Files\Python\lib\subprocess.py", line 644, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "C:\Program Files\Python\lib\lib-tk\Tkinter.py", line 592, in callit
func(*args)
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\_backend_tk.py", line 310, in idle_draw
self.draw()
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 12, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_agg.py", line 437, in draw
self.figure.draw(self.renderer)
File "C:\Program Files\Python\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Program Files\Python\lib\site-packages\matplotlib\figure.py", line 1484, in draw
**self._tight_parameters)
File "C:\Program Files\Python\lib\site-packages\matplotlib\figure.py", line 2307, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Program Files\Python\lib\site-packages\matplotlib\tight_layout.py", line 349, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Program Files\Python\lib\site-packages\matplotlib\tight_layout.py", line 115, in auto_adjust_subplotpars
if ax.get_visible()])
File "C:\Program Files\Python\lib\site-packages\matplotlib\axes\_base.py", line 4192, in get_tightbbox
bb.append(self.title.get_window_extent(renderer))
File "C:\Program Files\Python\lib\site-packages\matplotlib\text.py", line 924, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Program Files\Python\lib\site-packages\matplotlib\text.py", line 309, in _get_layout
ismath=ismath)
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_agg.py", line 236, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 501, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 365, in make_dvi
texfile], tex)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 335, in _run_checked_subprocess
stderr=subprocess.STDOUT)
File "C:\Program Files\Python\lib\subprocess.py", line 216, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Program Files\Python\lib\subprocess.py", line 394, in __init__
errread, errwrite)
File "C:\Program Files\Python\lib\subprocess.py", line 644, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Matplotlib version
- Windows 10
- Matplotlib 2.2.3
- Matplotlib backend: TkAgg
- Python version: 2.7.15
- Jupyter version (if applicable): ---
- Other libraries: numpy
Matplotlib was installed through pip.
Activity
jklymak commentedon Sep 25, 2018
Have you installed latex?
I'm going to close because I think this is just a config error.
RushikeshAgashe commentedon Sep 25, 2018
This is not a fix for this issue but a suggestion.
Consider using \textdegree instead of \N{DEGREE SIGN} ?
Original -
ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)Suggested -
ax.set_ylabel(r'\textit{Velocity (\textdegree /sec)}', fontsize=16)ImportanceOfBeingErnest commentedon Sep 25, 2018
@RushikeshAgashe That is indeed a totally different problem. If that line of code does not work, it's probably because the example is run with python 2. One could then refer to the matplotlib 2.2.3 version of that example, which looks a bit different.
I think this line is especially there to show how to use unicode symbols with tex so I would suggest not to replace it.
fbarbuto commentedon Sep 26, 2018
@RushikeshAgashe , thanks for the suggestion, but the script still doesn't work.
fbarbuto commentedon Sep 26, 2018
@ImportanceOfBeingErnest , thanks for pointing that 2.2.3 version of the example, but it didn't work either.
dstansby commentedon Sep 26, 2018
Re-opening, because even if this is a config issue the error should be much clearer. @fbarbuto does the example from 2.2.3 give the same error you posted above?
ImportanceOfBeingErnest commentedon Sep 26, 2018
@fbarbuto As suggested above by @jklymak you need a working tex install in order to be able to use
rcParams['text.usetex'] = True.I don't think we can give support on installing tex here.
anntzer commentedon Sep 26, 2018
Something like #9639 (needs additional work, I know) would help with giving a
TeX needed, please install iterror message everywhere relevant.fbarbuto commentedon Sep 26, 2018
OK. MiKTeX installed and added to PATH. Still nothing but error messages and a blank Matplotlib plotting window.
The new error messages:
"""
F:\Documents\Programs\Python>python tex_demo.py
Traceback (most recent call last):
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_qt5.py", line 519, in _draw_idle
self.draw()
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_agg.py", line 437, in draw
self.figure.draw(self.renderer)
File "C:\Program Files\Python\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, args, **kwargs)
File "C:\Program Files\Python\lib\site-packages\matplotlib\figure.py", line 1484, in draw
**self._tight_parameters)
File "C:\Program Files\Python\lib\site-packages\matplotlib\figure.py", line 2307, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "C:\Program Files\Python\lib\site-packages\matplotlib\tight_layout.py", line 349, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "C:\Program Files\Python\lib\site-packages\matplotlib\tight_layout.py", line 115, in auto_adjust_subplotpars
if ax.get_visible()])
File "C:\Program Files\Python\lib\site-packages\matplotlib\axes_base.py", line 4192, in get_tightbbox
bb.append(self.title.get_window_extent(renderer))
File "C:\Program Files\Python\lib\site-packages\matplotlib\text.py", line 924, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "C:\Program Files\Python\lib\site-packages\matplotlib\text.py", line 309, in get_layout
ismath=ismath)
File "C:\Program Files\Python\lib\site-packages\matplotlib\backends\backend_agg.py", line 236, in get_text_width_height_descent
s, fontsize, renderer=self)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 501, in get_text_width_height_descent
dvifile = self.make_dvi(tex, fontsize)
File "C:\Program Files\Python\lib\site-packages\matplotlib\texmanager.py", line 362, in make_dvi
with Locked(self.texcache):
File "C:\Program Files\Python\lib\site-packages\matplotlib\cbook_init.py", line 2529, in enter
raise self.TimeoutError(err_str)
TimeoutError: LOCKERROR: matplotlib is trying to acquire the lock
u'C:\Users\Fausto\.matplotlib\tex.cache\.matplotlib_lock-'
and has failed. This maybe due to any other process holding this
lock. If you are sure no other matplotlib process is running try
removing these folders and trying again.
"""
I don't think it's necessary to attach a snapshot of the blank plotting window here. Just imagine a Matplotlib frame with title & icons & everything else but with no plot inside. Heavenly white.
Thanks for the support & ideas you're giving me. Much appreciated.
WeatherGod commentedon Sep 26, 2018
fbarbuto commentedon Sep 26, 2018
@WeatherGod , Yes, I did that.
jklymak commentedon Sep 26, 2018
Does
C:\Users\Fausto\.matplotlib\tex.cacheexist? DoesC:\Users\Fausto\.matplotlib\tex.cache\.matplotlib_lockexist? Suggest you simply delete the directoryC:\Users\Fausto\.matplotlib\tex.cacheand try again (make sure no other python is running).fbarbuto commentedon Sep 26, 2018
I fixed the problem and the script is now working. Tell you what I did.
Matplotlib complained about folder
C:\Users\Fausto\.matplotlib\tex.cache\.matplotlib_lock, which was supposedly locked. It was also suggested that I should remove.matplotlib_lockand try again, what I did. I then re-ran the script and MiKTeX promptly complained about the lack of some packages, and asked me whether I would like to install them. Once the missing packages were installed, the script worked.Thanks to everyone who helped me out with that.
juancaros commentedon May 18, 2019
I can get it to work as a script with no problem, but the jupyter notebook fails:
`---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\formatters.py in call(self, obj)
339 pass
340 else:
--> 341 return printer(obj)
342 # Finally look for special method names
343 method = get_real_method(obj, self.print_method)
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in (fig)
242
243 if 'png' in formats:
--> 244 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
245 if 'retina' in formats or 'png2x' in formats:
246 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
126
127 bytes_io = BytesIO()
--> 128 fig.canvas.print_figure(bytes_io, **kw)
129 data = bytes_io.getvalue()
130 if fmt == 'svg':
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
2047 orientation=orientation,
2048 dryrun=True,
-> 2049 **kwargs)
2050 renderer = self.figure._cachedRenderer
2051 bbox_artists = kwargs.pop("bbox_extra_artists", None)
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
508
509 """
--> 510 FigureCanvasAgg.draw(self)
511 renderer = self.get_renderer()
512
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in draw(self)
400 toolbar = self.toolbar
401 try:
--> 402 self.figure.draw(self.renderer)
403 # A GUI class may be need to update a window using this draw, so
404 # don't forget to call the superclass.
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
48 renderer.start_filter()
49
---> 50 return draw(artist, renderer, *args, **kwargs)
51 finally:
52 if artist.get_agg_filter() is not None:
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py in draw(self, renderer)
1638 try:
1639 self.tight_layout(renderer,
-> 1640 **self._tight_parameters)
1641 except ValueError:
1642 pass
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py in tight_layout(self, renderer, pad, h_pad, w_pad, rect)
2375 kwargs = get_tight_layout_figure(
2376 self, self.axes, subplotspec_list, renderer,
-> 2377 pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
2378 if kwargs:
2379 self.subplots_adjust(**kwargs)
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\tight_layout.py in get_tight_layout_figure(fig, axes_list, subplotspec_list, renderer, pad, h_pad, w_pad, rect)
364 subplot_list=subplot_list,
365 ax_bbox_list=ax_bbox_list,
--> 366 pad=pad, h_pad=h_pad, w_pad=w_pad)
367
368 if rect is not None:
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\tight_layout.py in auto_adjust_subplotpars(fig, renderer, nrows_ncols, num1num2_list, subplot_list, ax_bbox_list, pad, h_pad, w_pad, rect)
113 continue
114
--> 115 tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
116 if ax.get_visible()])
117 tight_bbox = TransformedBbox(tight_bbox_raw,
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\tight_layout.py in (.0)
114
115 tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
--> 116 if ax.get_visible()])
117 tight_bbox = TransformedBbox(tight_bbox_raw,
118 fig.transFigure.inverted())
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes_base.py in get_tightbbox(self, renderer, call_axes_locator, bbox_extra_artists)
4371 self.apply_aspect()
4372
-> 4373 bb_xaxis = self.xaxis.get_tightbbox(renderer)
4374 if bb_xaxis:
4375 bb.append(bb_xaxis)
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in get_tightbbox(self, renderer)
1140 ticks_to_draw = self._update_ticks(renderer)
1141
-> 1142 self._update_label_position(renderer)
1143
1144 # go back to just this axis's tick labels
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in _update_label_position(self, renderer)
1932 # get bounding boxes for this axis and any siblings
1933 # that have been set by
fig.align_xlabels()-> 1934 bboxes, bboxes2 = self._get_tick_boxes_siblings(renderer=renderer)
1935
1936 x, y = self.label.get_position()
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in _get_tick_boxes_siblings(self, renderer)
1917 for nn, axx in enumerate(grp.get_siblings(self.axes)):
1918 ticks_to_draw = axx.xaxis._update_ticks(renderer)
-> 1919 tlb, tlb2 = axx.xaxis._get_tick_bboxes(ticks_to_draw, renderer)
1920 bboxes.extend(tlb)
1921 bboxes2.extend(tlb2)
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py in _get_tick_bboxes(self, ticks, renderer)
1123 for tick in ticks:
1124 if tick.label1On and tick.label1.get_visible():
-> 1125 extent = tick.label1.get_window_extent(renderer)
1126 ticklabelBoxes.append(extent)
1127 if tick.label2On and tick.label2.get_visible():
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\text.py in get_window_extent(self, renderer, dpi)
928 raise RuntimeError('Cannot get window extent w/o renderer')
929
--> 930 bbox, info, descent = self._get_layout(self._renderer)
931 x, y = self.get_unitless_position()
932 x, y = self.get_transform().transform_point((x, y))
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\text.py in _get_layout(self, renderer)
311 w, h, d = renderer.get_text_width_height_descent(clean_line,
312 self._fontproperties,
--> 313 ismath=ismath)
314 else:
315 w, h, d = 0, 0, 0
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
207 fontsize = prop.get_size_in_points()
208 w, h, d = texmanager.get_text_width_height_descent(
--> 209 s, fontsize, renderer=self)
210 return w, h, d
211
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\texmanager.py in get_text_width_height_descent(self, tex, fontsize, renderer)
464 dvifile = self.make_dvi(tex, fontsize)
465 with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
--> 466 page = next(iter(dvi))
467 # A total height (including the descent) needs to be returned.
468 return page.width, page.height + page.descent, page.descent
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in iter(self)
242 integers.
243 """
--> 244 while self._read():
245 yield self._output()
246
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in _read(self)
300 while True:
301 byte = self.file.read(1)[0]
--> 302 self._dtable[byte](self, byte)
303 if byte == 140: # end of page
304 return True
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in wrapper(self, byte)
160 if state is not None and self.state != state:
161 raise ValueError("state precondition failed")
--> 162 return method(self, *[f(self, byte-min) for f in get_args])
163 if max is None:
164 table[min] = wrapper
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in _fnt_def(self, k, c, s, d, a, l)
440 @_dispatch(min=243, max=246, args=('olen1', 'u4', 'u4', 'u4', 'u1', 'u1'))
441 def _fnt_def(self, k, c, s, d, a, l):
--> 442 self._fnt_def_real(k, c, s, d, a, l)
443
444 def _fnt_def_real(self, k, c, s, d, a, l):
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in _fnt_def_real(self, k, c, s, d, a, l)
445 n = self.file.read(a + l)
446 fontname = n[-l:].decode('ascii')
--> 447 tfm = _tfmfile(fontname)
448 if tfm is None:
449 raise FileNotFoundError("missing font metrics file: %s" % fontname)
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in _fontfile(cls, suffix, texname)
1027 @lru_cache()
1028 def _fontfile(cls, suffix, texname):
-> 1029 filename = find_tex_file(texname + suffix)
1030 return cls(filename) if filename else None
1031
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py in find_tex_file(filename, format)
1019 cmd += [filename]
1020 _log.debug('find_tex_file(%s): %s', filename, cmd)
-> 1021 pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
1022 result = pipe.communicate()[0].rstrip()
1023 _log.debug('find_tex_file result: %s', result)
C:\ProgramData\Anaconda3\lib\subprocess.py in init(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
773 c2pread, c2pwrite,
774 errread, errwrite,
--> 775 restore_signals, start_new_session)
776 except:
777 # Cleanup if the child failed starting.
C:\ProgramData\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
1176 env,
1177 os.fspath(cwd) if cwd is not None else None,
-> 1178 startupinfo)
1179 finally:
1180 # Child is launched. Close the parent's copy of those pipe
FileNotFoundError: [WinError 2] The system cannot find the file specified
`
tacaswell commentedon May 18, 2019
@juancaros That likely means the installation of latex is visible in the context where you are running it as a script, but not in the context of where the jupyter kernel is running.
juancaros commentedon May 18, 2019
Oddly, I make it work by ignoring the usetex parameter on matplotlib as suggested in other forum.