Skip to content

Unicode with usetex=True and pgf backend #5234

Closed
@itoijala

Description

@itoijala

The documentation says that registering the backend should work as well (or better) than use('pgf').
This breaks when using unicode because the TeX strings are handled by the default matplotlib texmanager instead of the pgf backend.

If there is no way to disable the standard texmanager when rendering using the pgf backend, the documentation should be changed to make this problem clear.

import matplotlib as mpl
#mpl.use("pgf")   # works
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pgf import FigureCanvasPgf  # does not work
mpl.backend_bases.register_backend('pdf', FigureCanvasPgf)   #
mpl.rcParams.update({
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
    'pgf.preamble': r'\usepackage{unicode-math}',
})
plt.plot([1, 2], [3, 4])
plt.xlabel(r'$α$')
plt.savefig('foo.pdf')
Traceback (most recent call last):
  File "baz.py", line 19, in <module>
    plt.savefig('baz.pdf')
  File "/usr/lib/python3.5/site-packages/matplotlib/pyplot.py", line 577, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/figure.py", line 1476, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 162, in print_figure
    self.draw()
  File "/usr/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 148, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 469, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3.5/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/figure.py", line 1085, in draw
    func(*args)
  File "/usr/lib/python3.5/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2110, in draw
    a.draw(renderer)
  File "/usr/lib/python3.5/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/axis.py", line 1128, in draw
    self.label.draw(renderer)
  File "/usr/lib/python3.5/site-packages/matplotlib/artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.5/site-packages/matplotlib/text.py", line 595, in draw
    bbox, info, descent = self._get_layout(renderer)
  File "/usr/lib/python3.5/site-packages/matplotlib/text.py", line 320, in _get_layout
    ismath=ismath)
  File "/usr/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 223, in get_text_width_height_descent
    renderer=self)
  File "/usr/lib/python3.5/site-packages/matplotlib/texmanager.py", line 670, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/usr/lib/python3.5/site-packages/matplotlib/texmanager.py", line 394, in make_dvi
    texfile = self.make_tex(tex, fontsize)
  File "/usr/lib/python3.5/site-packages/matplotlib/texmanager.py", line 309, in make_tex
    fh.write(s.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode character '\u03b1' in position 298: ordinal not in range(128)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions