使用tex作为pdf时保存matplotlib pyplot

时间:2018-01-26 16:27:24

标签: python-3.x matplotlib

我正在使用matplotlib创建一个情节。该图表在jupyter笔记本中正确显示,但我无法将其另存为pdf。保存为png可以正常工作,但遗憾的是这对我来说不是一个选择。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

values= [[0,   -1.055], [2.5, -1.798], [6.5, -1.662], [9,   -1.274],
        [10,  -0.939], [11,  -0.527], [12,  -0.338], [15,   0.345],
        [17,  -0.146], [19,  -1.526], [19,  -1.395], [21,  -2.454]]

values= np.array(values)

fig = plt.figure(1, figsize=(9,6.5))

plt.rc('text', usetex=True)
plt.rc('font', family='serif')

plt.scatter(values[:,0], values[:,1])
plt.show()

pdf = PdfPages('test.pdf')
pdf.savefig(fig)
pdf.close()

我收到以下错误:

TypeError: a bytes-like object is required, not 'str'

完成错误转储:https://pastebin.com/rFTr3ehU

我正在运行python 3.6.3 | Anaconda自定义(64位)| (默认,2017年10月15日,03:27:45)[MSC v.1900 64 bit(AMD64)]和matplotlib 2.1.0版。

1 个答案:

答案 0 :(得分:2)

也许这有帮助?

有这种修复类型错误: https://github.com/matplotlib/matplotlib/pull/9292/commits/37ad476aec62ce58f95c91e6aabd55cfc3b5af25

这里也遇到了同样的问题: https://github.com/matplotlib/matplotlib/issues/9671

要么获得固定版本,要么降级为:Matplot 2.0.2