我已经准备了一本朱庇特笔记本和一些材料库的情节。现在,我想将绘图保存为PGF格式,这样我就可以在LaTeX中重用它们。我跟随这篇博客文章来实现这个想法。
不幸的是,如果我将matplotlib配置为生成PGF文件,它们将不会显示在笔记本中。如果禁用matplotlib来生成PGF文件,则会显示绘图,但不生成PGF文件。我怎么能两者兼得呢?
下面是重现问题的最小示例:
# Test if integration between python and latex works fine
import subprocess; subprocess.check_call(["latex", "-help"])
# Configure matplotlib to generate PGF files
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("pgf")
matplotlib.rcParams.update({
"pgf.texsystem": "pdflatex",
'font.family': 'serif',
'text.usetex': True,
'pgf.rcfonts': False,
})
# Prepare simple plot
import pandas as pd
df = pd.DataFrame({
'length': [1.5, 0.5, 1.2, 0.9, 3],
'width': [0.7, 0.2, 0.15, 0.2, 1.1]
}, index= ['pig', 'rabbit', 'duck', 'chicken', 'horse'])
hist = df.hist(bins=3)
# Save the plot to PGF file
plt.savefig('histogram.pgf')以下是matplotlib已启用配置的输出。生成并保存histogram.pgf文件,但未显示绘图。

下面是为PGF禁用matplotlib配置的示例。显示了绘图,但是生成的histogram.pgf文件是空的

发布于 2020-01-24 23:49:15
我不能重复你的问题


我必须运行第一个牢房三到四次,否则我就看不到那个数字了。
%matplotlib inline
顺便说一下,我使用的是Ubuntu16.04、Python3.7.6和Matplotlib 3.1.1。
发布于 2021-09-13 05:32:02
不要使用matplotlib.use("pgf");有一种替代方法可以实现同样的目标,而不会出现无法显示您的情节的缺点:
from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)该方法允许您继续使用常规的交互式后端,同时能够使用plt.savefig('figure.pdf')1保存已编译的PDF文件,并且在保存文件后,将以内联方式显示绘图。
您仍然可以设置pgf参数。没有必要删除它们,因为它们不会干扰交互式后端:
matplotlib.rcParams.update({
"pgf.texsystem": "pdflatex",
'pgf.rcfonts': False,
})https://stackoverflow.com/questions/59892762
复制相似问题