从Jupyter Notebook显示和保存PGF图

问题描述 投票:3回答:1

我准备了一个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文件已生成并保存,但未显示图。

With configuration enabled

这里是为PGF禁用matplotlib配置的示例。显示了图,但生成的histogram.pgf文件为空---不包含图。

enter image description here

python matplotlib jupyter-notebook latex pgf
1个回答
0
投票

我无法重现您的问题

enter image description here

enter image description here

我必须运行第一个单元三到四次,否则我将看不到该图。

%matplotlib inline

顺便说一句,我正在使用Ubuntu 16.04,Python 3.7.6和Matplotlib 3.1.1。

© www.soinside.com 2019 - 2024. All rights reserved.