Repository navigation
Closed
Milestone
Description
The following script:
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import math
from matplotlib import rc
rc('font',**{'family':'serif'})
rc('text', usetex=True)
rc('text.latex',unicode=True)
rc('text.latex',preamble=r'\usepackage[utf8]{inputenc}')
rc('text.latex',preamble=r'\usepackage[russian]{babel}')
def figsize(wcm,hcm): plt.figure(figsize=(wcm/2.54,hcm/2.54))
figsize(13,9)
x = np.linspace(0,2*math.pi,100)
y = np.sin(x)
plt.plot(x,y,'-')
plt.xlabel(u"Ось абсцисс")
plt.show()
works fine and shows the figure, but saving to pdf results in the following error:
Traceback (most recent call last):
File "C:\Path\rus pics\___test_rus.py", line 22, in <module>
plt.savefig(u"c:/fig.pdf")
File "C:\Python35\lib\site-packages\matplotlib\pyplot.py", line 688, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\figure.py", line 1565, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\backend_bases.py", line 2232, in print_figure
**kwargs)
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2536, in print_pdf
self.figure.draw(renderer)
File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\figure.py", line 1159, in draw
func(*args)
File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\axes\_base.py", line 2324, in draw
a.draw(renderer)
File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\axis.py", line 1120, in draw
self.label.draw(renderer)
File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\text.py", line 792, in draw
mtext=mtext)
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1866, in draw_tex
psfont = self.tex_font_mapping(dvifont.texname)
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1568, in tex_font_mapping
return self.tex_font_map[texfont]
File "C:\Python35\lib\site-packages\matplotlib\dviread.py", line 701, in __getitem__
result = self._font[texname.decode('ascii')]
AttributeError: 'str' object has no attribute 'decode'
The error arises only if cyrillic letters are used in labels.
Matplotlib 1.5.1, Python 3.5, OS Windows 10
Stackoverflow question link
Activity
[-]savefig tp pdf: 'str' object has no attribute 'decode'[/-][+]savefig to pdf: 'str' object has no attribute 'decode'[/+]tacaswell commentedon May 31, 2016
I get different errors, but I assume I am missing the required russian specific tex packages.
Can you drop into the debugger and get what the font name is? It looks like this is a work-around to getting bytestrings that need to be decoded to
strbut it is already getting in astr.attn @mdboom
tacaswell commentedon May 31, 2016
also @jkseppan
jkseppan commentedon Jun 1, 2016
I'm guessing that the Cyrillic font does not have an entry in pdftex.map, which would make this another instance of #4167.
horribleheffalump commentedon Jun 1, 2016
I've tried the same code under Python 2.7 and got another error:
larm1200 that's the font name
jkseppan commentedon Jun 18, 2016
I don't get this error on current master, but the line of text is cut off in the middle.

jkseppan commentedon Jun 18, 2016
FWIW, I've got TeX Live 2015 on OS X 10.11. The font called larm1200 by TeX is supplied in the file sfrm1200.pfb.
@horribleheffalump: Do you have an entry for larm1200 in pdftex.map? Is there a corresponding pfa or pfb file?
horribleheffalump commentedon Jun 19, 2016
@jkseppan Yes! That was the problem. I've installed sfrm1200.pfb as a part of cm-super package, and now it all works fine. Thank you!
cwberardi commentedon Aug 25, 2016
The following script is giving me the same error:
Which produces the following error:
However, I am not using any special characters except [ '%', '$'], which I have escaped. The figure will show fine on the console. The error occurs when I attempt to save as a .pdf.
Matplotlib 1.5.1, Python 3.4, OS Windows 7
Handle dvi font names as ASCII bytestrings
Add a helpful note when raising KeyError from dviread.PsFonts
cwberardi commentedon Aug 25, 2016
@jkseppan thanks for quick review. Stackoverflow link should you have any suggestions an a temporary work around.
jkseppan commentedon Aug 26, 2016
See pull request #6977 for a fix that improves the error message so you know which font is missing. You probably need to install some PS font package in your TeX package manager.
Handle dvi font names as ASCII bytestrings
Add a helpful note when raising KeyError from dviread.PsFonts
Handle dvi font names as ASCII bytestrings
Add a helpful note when raising KeyError from dviread.PsFonts
tacaswell commentedon Oct 21, 2017
This was fixed by #6977 and went out with 2.1