8000 [Bug]: `UnicodeDecodeError` when using some special and accented characters in TeX · Issue #23019 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: UnicodeDecodeError when using some special and accented characters in TeX #23019
Closed
@23ccozad

Description

@23ccozad

Bug summary

I'm just getting started with the development side of matplotlib, and I'm getting a UnicodeDecodeError in some cases (not all cases) when TeX is being used in unit tests and building docs. The cause of the issue may be specific to my installation and seems to be isolated to the use of certain special and accented characters, but I'm not sure how to resolve it.

The code below is an example (from tex_demo.py in the docs) of one instance where this error is occurring, and \N{DEGREE SIGN} is causing the issue (removing it allows the code to run without error).

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['text.usetex'] = True

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)  # \N{DEGREE SIGN} appears to be the culprit
ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
             r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')

Actual outcome

Traceback from warning/error in building docs:

generating gallery for gallery\text_labels_and_annotations... [ 78%] tex_demo.pydemo.py.pyxample.py
Warning, treated as error:
C:\Users\username\Documents\GitHub\matplotlib\examples\text_labels_and_annotations\tex_demo.py failed to execute correctly: Traceback (most recent call last):

  File "C:\Users\username\miniconda3\envs\matplotlib-dev\lib\site-packages\sphinx_gallery\scrapers.py", line 378, in save_figures
    rst = scraper(block, block_vars, gallery_conf)
  File "C:\Users\username\Documents\GitHub\matplotlib\doc\conf.py", line 173, in matplotlib_reduced_latex_scraper
    return matplotlib_scraper(block, block_vars, gallery_conf, **kwargs)
  File "C:\Users\username\miniconda3\envs\matplotlib-dev\lib\site-packages\sphinx_gallery\scrapers.py", line 171, in matplotlib_scraper
    fig.savefig(image_path, **these_kwargs)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\figure.py", line 3099, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\backend_bases.py", line 2262, in print_figure
    self.figure.draw(renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\artist.py", line 73, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\figure.py", line 2891, in draw
    mimage._draw_list_compositing_images(
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\axes\_base
.py", line 3042, in draw
    self._update_title_position(renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\axes\_base
.py", line 2982, in _update_title_position
    ax.yaxis.get_tightbbox(renderer)  # update offsetText
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\text.py", line 915, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\text.py", line 321, in _get_layout                   File "c:\users\username\documents\github\matplotlib\lib\matplotlib\axis
6692
.py",
line 1244, in get_tightbbox
    bb = self.label.get_window_extent(renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\text.py",
line 915, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\text.py",
line 321, in _get_layout
    w, h, d = _get_text_metrics_with_cache(
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\text.py", line 97, in _get_text_metrics_with_cache
    return _get_text_metrics_with_cache_impl(
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\text.py", line 105, in _get_text_metrics_with_cache_impl
    return renderer_ref().get_text_width_height_descent(text, fontprop, ismath)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\backends\backend_agg.py", line 235, in get_text_width_height_descent
    w, h, d = texmanager.get_text_width_height_descent(
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\texmanager.py", line 359, in get_text_width_height_descent
    dvifile = cls.make_dvi(tex, fontsize)
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\texmanager.py", line 292, in make_dvi
    cls._run_checked_subprocess(
  File "c:\users\username\documents\github\matplotlib\lib\matplotlib\texmanager.py", line 268, in _run_checked_subprocess
    exc=exc.output.decode('utf-8'))) from exc
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 1673: invalid start byte

The test_savefig_to_stringio() test also fails four times, each time with a different set of arguments. This is also a UnicodeDecodeError, but it originates from the title of the plot having accented characters ("Déjà vu"). Removing those characters resolves the UnicodeDecodeError, but leaves me with an AssertionError at the end of the test. Traceback is omitted here because it is over 200 lines long, and results in a similar error as above.

Expected outcome

Expected outcome is to build docs and pass all tests without failure. Referring to the failing instance in the docs from tex_demo.py, that code should produce:

Additional information

Again, this could be isolated to my installation of matplotlib, but if anyone can reproduce this error or knows of a possible fix, that would be great.

Operating system

Windows 11

Matplotlib Version

3.6.0.dev2170+g40676bb62f

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.8.13

Jupyter version

3.4.0

Installation

git checkout

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0