我有以下腳本來生成與matplotlib一個數字:錯誤節省matplotlib的數字爲pdf:「STR」對象有沒有屬性「解碼」
# -*- 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()
它工作正常,這一數字是正確呈現。但是,當我試圖將其保存爲PDF格式,我得到以下錯誤:
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'
只有當我使用標籤西裏爾字母的錯誤出現。
請使用'** {'family':'seif'}顯示整個回溯信息 –
似乎有點傻,因爲您可以指定關鍵字,就像:'rc('font',family ='serif' ) –
添加完整跟蹤 –