8000 UnicodeDecodeError when making a plot using the 'classic' style and text.usetex=True · Issue #8423 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
UnicodeDecodeError when making a plot using the 'classic' style and text.usetex=True #8423
Closed
@ngoldbaum

Description

@ngoldbaum

Bug report

When making a plot using the 'classic' style, matplotlib overrides the default value for text.latex.unicode, setting it to False. This causes a problem for any plot that has negative numbers in any label, because matplotlib now uses a unicode symbol to represent the negative sign.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
rcParams['text.usetex'] = True

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

plt.plot(t, s)

with plt.style.context('classic'):
    plt.savefig('tex_demo')

Actual outcome

The following traceback:

  File "test_mpl.py", line 14, in <module>
    plt.savefig('tex_demo')
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/pyplot.py", line 707, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/figure.py", line 1763, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/backend_bases.py", line 2234, in print_figure
    **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/backends/backend_agg.py", line 548, in print_png
    FigureCanvasAgg.draw(self)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/backends/backend_agg.py", line 467, in draw
    self.figure.draw(self.renderer)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/artist.py", line 68, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/figure.py", line 1240, in draw
    renderer, self, artists, self.suppressComposite)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/artist.py", line 68, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/axes/_base.py", line 2386, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/artist.py", line 68, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/axis.py", line 1118, in draw
    tick.draw(renderer)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/artist.py", line 68, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/axis.py", line 268, in draw
    self.label1.draw(renderer)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/artist.py", line 68, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/text.py", line 797, in draw
    mtext=mtext)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/backends/backend_agg.py", line 255, in draw_tex
    Z = texmanager.get_grey(s, size, self.dpi)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/texmanager.py", line 585, in get_grey
    pngfile = self.make_png(tex, fontsize, dpi)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/texmanager.py", line 508, in make_png
    dvifile = self.make_dvi(tex, fontsize)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/texmanager.py", line 400, in make_dvi
    texfile = self.make_tex(tex, fontsize)
  File "/Users/goldbaum/Documents/matplotlib-git/lib/matplotlib/texmanager.py", line 311, in make_tex
    if rcParams['text.latex.unicode']:
UnicodeEncodeError: 'ascii' codec can't encode character '\u2212' in position 298: ordinal not in range(128)

Expected outcome

The plot should be saved to disk without erroring. The script runs without error on matplotlib 1.5.3.

Matplotlib version

Matplotlib 2.0.0 on OSX, installed via pip.

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