我准备了一个Jupyter笔记本,其中包含许多matplotlib图。现在,我想将图保存为PGF格式,以便可以在LaTeX中重用它们。我遵循this blog post来实现该想法。
[不幸的是,如果我将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的enabled配置的输出。 histogram.pgf
文件已生成并保存,但未显示图。
这里是为PGF禁用matplotlib配置的示例。显示了图,但生成的histogram.pgf
文件为空---不包含图。