我在一个使用MatplotLib绘制和保存简单图形的项目中工作。但自上次更新Windows以来,如果我使用Latex格式的文本,我无法以pdf格式保存图片。请参阅下面的Spyder绘图试验:
import numpy as np
from matplotlib import pyplot as plt
# Data for plot
step = 0.001
x = np.arange(0, 2 + step, step)
y = x*x*np.cos(10*x)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.figure(figsize=(19.2, 10.8))
plt.plot(x, y, lw=2)
plt.title(r'Function $f(x) = x^2 \cos(10x)$')
plt.savefig('plot.pdf', bbox_inches='tight')
plt.show()
plt.close()
此代码生成以下错误:
Traceback (most recent call last):
File "<ipython-input-25-375950fa7d9a>", line 1, in <module>
runfile('C:/Users/Fábio/Desktop/plotter.py', wdir='C:/Users/Fábio/Desktop')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Fábio/Desktop/plotter.py", line 15, in <module>
plt.savefig('plot.pdf', bbox_inches='tight')
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 701, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1834, in savefig
self.canvas.print_figure(fname, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2216, in print_figure
**kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2592, in print_pdf
self.figure.draw(renderer)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1299, in draw
renderer, self, artists, self.suppressComposite)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 2437, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py", line 1138, in draw
tick.draw(renderer)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axis.py", line 282, in draw
self.label1.draw(renderer)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\text.py", line 762, in draw
mtext=mtext)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1944, in draw_tex
pdfname = self.file.dviFontName(dvifont)
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 688, in dviFontName
psfont = self.texFontMap[dvifont.texname]
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\backends\backend_pdf.py", line 673, in texFontMap
dviread.find_tex_file('pdftex.map'))
File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\dviread.py", line 1057, in find_tex_file
return result.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)
上面的代码没有任何问题,可以用不同的格式保存(.png和.eps,例如)但是崩溃了.pdf。在更新Windows并使用相同的Latex和Python配置之前,此保存功能非常适合在.pdf中保存。
所以我请你帮我解决这个问题,如果可能的话。
Ps。:作为示例的代码在Spyder 3.2.6中使用Python 3.6.4进行了测试。使用的Latex工作区包含Miktex的基础包,可以完美地保存.pdf中的doc或.pdf中转换.png / .eps数据
不用多说,我非常感谢你的关注。
答案 0 :(得分:0)
最有可能是由于Windows用户名文件夹中包含国家/地区特定字符(á)。
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
colors: {
blue: '#1f96ff',
green: '#0DAA54',
orange: '#F49D3A',
red: '#FF2833',
purple: '#5E57BA',
pink: '#B539AC'
},
priorities: [
{
id: 1,
name: 'Low',
value: 10,
color: THIS SHOUD LINK TO colors.green
},
{
id: 2,
name: 'Medium',
value: 20,
color: THIS SHOUD LINK TO colors.orange
},
{
id: 3,
name: 'High',
value: 30,
color: THIS SHOUD LINK TO colors.red
}
}
}
无法对此进行解码,并且不会为您的return result.decode('ascii')
生成有效路径,该路径包含matlibplot生成乳胶字体所需的所有信息。
通过将matlibplot库中的.tfm-files
文件更改为divread.py
,应该可以解决此问题。我不知道这是否会破坏其他任何依赖关系,但对我来说效果很好。