在matplotlib中将savefig与text.usetex:true一起使用时,我得到TypeError: a bytes-like object is required, not 'str'
编辑:对我来说,解决方案是将matplotlib从2.1.0升级到3.0.2
我可以使用usetex:true保存为png格式,但不能保存为pdf。我可以使用usetex:false将其保存为pdf。这是产生错误所需的最少代码:
import matplotlib as mpl
mpl.use('pdf')
import matplotlib.pyplot as plt
mpl.rcParams['text.usetex'] = True
fig, axes = plt.subplots(1, 1)
fig.savefig('test.pdf')
我安装了Miktex 2.9,还安装了dvipng和Ghostscript的最新版本,如此处所述:https://matplotlib.org/tutorials/text/usetex.html?highlight=usetex
我在路径中添加了以下内容:
C:\Program Files\gs\gs9.26\bin
C:\Program Files\MiKTeX 2.9\miktex\bin\x64
我正在使用Windows 10,Python 3.6.3和matplotlib 2.1.0。
我也尝试删除.matplotlib / tex.cache目录,但没有成功。
完整追溯:
File ".\usetex.py", line 9, in <module>
fig.savefig('test.pdf')
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\figure.py", line 1814, in savefig
self.canvas.print_figure(fname, **kwargs)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backend_bases.py", line 2259, in print_figure
**kwargs)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2592, in print_pdf
self.figure.draw(renderer)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\figure.py", line 1295, in draw
renderer, self, artists, self.suppressComposite)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\axes\_base.py", line 2399, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\axis.py", line 1138, in draw
tick.draw(renderer)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\axis.py", line 282, in draw
self.label1.draw(renderer)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\text.py", line 799, in draw
mtext=mtext)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1944, in draw_tex
pdfname = self.file.dviFontName(dvifont)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\backends\backend_pdf.py", line 688, in dviFontName
psfont = self.texFontMap[dvifont.texname]
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\dviread.py", line 866, in __getitem__
fn = find_tex_file(fn)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\site-packages\matplotlib\dviread.py", line 1045, in find_tex_file
stderr=subprocess.PIPE)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 971, in _execute_child
args = list2cmdline(args)
File "C:\Users\bob\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 461, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: a bytes-like object is required, not 'str'
答案 0 :(得分:0)
由于@ImportanceOfBeingErnest,我将matplotlib从2.1.0升级到3.0.2,该错误不再显示。