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

asked 2024-02-22 19:08:10 +0200

stillconfused gravatar image

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!


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")
edit retag flag offensive close merge delete


@stillconfused: WorksForMe(TM) on 10.3.beta8 compiled from source git on Linux Debian testing ; my LaTeX installation is Debian's texlive-full.

What is your platform, and how did you install Sage ? Under Windows, the use of a Windows-installed LaTeX by WSL's sagemath may be problematic (or at least non-obvious to configure...).

Since this is more a Python/Matplotlib question than a Sage question, querying relevant channels may be more useful.

Acid test : can you run

!pdflatex --version

at the Sage prompt ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-02-23 12:27:05 +0200 )edit