简体   繁体   English

我无法使用Matplotlib将其以pdf格式保存

[英]I can not save a plot in pdf format using Matplotlib

I work in a project that uses MatplotLib for plotting and saving simple graphics. 我在一个使用MatplotLib绘制和保存简单图形的项目中工作。 But since last update of Windows, if I use text in Latex format, I can not save a picture in pdf format. 但是自从Windows的上一次更新以来,如果我使用Latex格式的文本,则无法将图片保存为pdf格式。 See below a trial of plotting in Spyder: 参见下面的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()

This code generate the follow error: 此代码生成以下错误:

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)

The code above work without any problem for save in different formats (.png and .eps, eg) but crash with .pdf. 上面的代码可以毫无问题地保存为不同格式(例如.png和.eps),但是与.pdf崩溃。 Before the update of Windows and using tha same configuration for Latex and Python, this save function work perfect for save in .pdf. 在Windows更新之前,对于Latex和Python使用相同的配置,此保存功能非常适合保存为.pdf。

So I ask that you help me solve this problem, if possible. 因此,如果可以的话,请您帮助我解决这个问题。

Ps.: The code as exemple was tested in Spyder 3.2.6 with Python 3.6.4. 附:示例代码已在Spyder 3.2.6和Python 3.6.4中进行了测试。 The Latex workspace used contains the basics packages from Miktex and work perfectly for save the doc in .pdf or transform a .png / .eps figure in .pdf 所使用的Latex工作区包含Miktex的基础软件包,可以完美地将文档保存为.pdf或将.png / .eps图形转换为.pdf

Without further ado, I thank you immensely for your attention. 事不宜迟,我非常感谢您的关注。

This most likely occurs due to your Windows user name folder containing country specific characters (á). 由于您的Windows用户名文件夹包含特定于国家的字符(á),因此最有可能发生这种情况。

The return result.decode('ascii') cannot decode this and will not generate a valid path to your .tfm-files , which contains all the information needed by matlibplot to generate the latex font. return result.decode('ascii')无法对此进行解码,也不会生成指向.tfm-files的有效路径,该路径包含matlibplot生成乳胶字体所需的所有信息。

By changing the divread.py file in the matlibplot library to result.decode('utf-8') , the problem should be fixed. 通过将matlibplot库中的divread.py文件更改为result.decode('utf-8') ,即可解决此问题。 I do not know if this breaks any other dependencies, but it works well for me. 我不知道这是否会破坏其他任何依赖关系,但对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用matplotlib在python中保存一个图? - How can I save a plot in python using matplotlib? 我可以将matplotlib动画保存为webm格式吗? - Can I save a matplotlib animation to webm format? 如何在 Python 中使用 matplotlib 在绘图中的所有 x 值的 xaxis 中以小时和分钟格式显示标签? - How can I show labels in hours and minutes format in xaxis for all x values in a plot using matplotlib in Python? 如何保存 matplotlib plot 即 output 的 output in jupyC425268E68385D1AB507 - How can I save a matplotlib plot that is the output of a function in jupyter? Matplotlib pdf 后台无法用usetex保存pdf - Matplotlib pdf backend can not save pdf with usetex 无法使用 Matplotlib 保存决策树 plot - Cannot save decision tree plot using Matplotlib 使用 Matplotlib 以 pdf 格式保存绘图时出现奇怪的消息,为什么? - Strange message when saving a plot in pdf format with Matplotlib, why? 我如何使用matplotlib用timedelta绘制图形 - how can i plot a graph with timedelta using matplotlib 如何使用 matplotlib 在 statsmodel 中绘制 Logit 的结果 - How can I plot the results of Logit in statsmodel using matplotlib 如何在matplotlib中使用一个数据框绘制两个图? - How can I plot two graphs using one dataframe in matplotlib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM