Ask Your Question

Revision history [back]

Exporting pgf plots for latex from Sage environment using pandas and matplotlib

I've done some data analysis using Pandas in Sage, and would like to export plots to include in a paper. Unfortunately, exporting as a pdf means that titles etc. won't adjust to changes in the formatting of my Latex document, so I would like to export as a pgf (as explained, for example, here).

When I do this, however, I get the following error:

File ~/sage/sage/local/var/lib/sage/venv-python3.10/lib/python3.10/site-packages/matplotlib/backends/backend_pgf.py:293, in LatexManager.__init__(self)
    290     self._setup_latex_process(expect_reply=False)
    291 except FileNotFoundError as err:
    292     raise RuntimeError(
--> 293         f"{self.latex.args[0]!r} not found.  Install it or change "
    294         f"rcParams['pgf.texsystem'] to an available TeX "
    295         f"implementation.") from err
    296 except OSError as err:
    297     raise RuntimeError(
    298         f"Error starting process {self.latex.args[0]!r}") from err

AttributeError: 'LatexManager' object has no attribute 'latex'

It seems that somehow matplotlib is not able to find the correct latex implementation, but I'm not sure how to fix that. Does anyone do this regularly and know of a solution? If it's relevant, I am running Sage 10.0.beta6 on WSL. I also have a MWE below for reference. Thanks so much!

MWE:

sage: import matplotlib
sage: import matplotlib.pyplot as plt
sage: matplotlib.use('pgf')
sage: import pandas as pd
sage: df = pd.DataFrame({'length' : [float(x) for x in [1.5,0.5,1.2,0.9,3]],
....:  'width' : [float(x) for x in [0.7, 0.2, 0.15, 0.2, 1.1]]}, index = ['
....: pig', 'rabbit', 'duck', 'chicken', 'horse'])
sage: hist = df.hist(bins = 3)
sage: plt.savefig("test.pgf")