Python 使用plot和latex保存pdf

Python 使用plot和latex保存pdf,python,matplotlib,latex,Python,Matplotlib,Latex,我想用Latex中的字体保存绘图,但出现错误: TypeError:需要类似字节的对象,而不是“str” 我通过以下方式初始化pyplot中的Latex: plt.rc('text', usetex=True) plt.rc('font', family='serif') 并通过以下方式保存pdf: fig.savefig('myplot.pdf', transparent=True) 将所有内容保存到png works,仅pdf失败。有什么想法吗?尝试从matplotlib导入Pdfpag

我想用Latex中的字体保存绘图,但出现错误:

TypeError:需要类似字节的对象,而不是“str”

我通过以下方式初始化pyplot中的Latex:

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
并通过以下方式保存pdf:

fig.savefig('myplot.pdf', transparent=True)

将所有内容保存到png works,仅pdf失败。有什么想法吗?

尝试从matplotlib导入Pdfpages,并按如下方式实现:

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

    fig = plt.figure()
    pdf = PdfPages('foo.pdf')
    pdf.savefig(fig)

    pdf.close()
对我来说,来自中国的解决方案奏效了

通过在后面添加以下行来调整matplotlib脚本 导入matplotlib:


您确定已使用disable TeX Works保存为png或pdf吗?您需要提供一个包含完整错误回溯的文件。否则这个问题就没用了。可能是重复的
matplotlib.use("pgf")
matplotlib.rcParams.update({
    "pgf.texsystem": "pdflatex",
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
})