I have prepared a Jupyter Notebook with a number of matplotlib plots. Now I would like to save plots to PGF format, so I can reuse them in LaTeX. I have followed this blog post to implement that idea.
Unfortunately, if I configure matplotlib to generate PGF files they are not shown in the notebook. If I disable matplotlib to generate PGF files plots are shown but PGF files are not generated. How can I have both?
Here's minimal example to reproduce the problem:
# 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')
Here's the output for enabled configuration of matplotlib. histogram.pgf
file is generated and saved, but plot is not shown.
Here's example of when disabling matplotlib configuration for PGF. Plot is shown, but generated histogram.pgf
file is empty --- does not contain the plot.