figure generation a la gnuplot: graphics and latex files separately

Your suggestion did work (when adding a colon after pgf.preamble, in the
matplotlibrc file)

Ouch!

1) in the main tex file I added some surrounding text, to be able to check
the matching of the fonts and it looked as if the family font were ok, but
the size of the labels (xlabel and ylabel) as well as of the tick marker
labels (numbers) were distinct (bigger) than of the surrounding main body
text...

Oh, I'd forgotten that:

  matplotlibrc contents:
  [...]
  backend : Qt4Agg
  font.size : 10.0
  pgf.rcfonts : False
  pgf.texsystem : pdflatex
  pgf.preamble : \usepackage{/dev/shm/foo/foo}
  [...]

Yes, you have to set the same basic font size as you are going to use in your
document, it is not fully automatic. If you are using something like
\documentclass[12pt]{article}, you have to set font.size : 12.0. You can
programmatically change font.size from within the python script after parsing
the main tex file:

#!/usr/bin/env python

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import re

with open('foo.tex') as f:
    for line in f:
        if 'documentclass' in line:
            try:
                match = re.search('[0-9]+pt', line).group(0)
                font_size = float(match.replace('pt', ''))
            except:
                font_size = 10.

mpl.rcParams.update({'font.size': font_size})
x = np.linspace(0, 4. * np.pi)
y = np.sin(x) ** 2
fig = plt.figure(figsize=(3, 3))
ax = plt.gca()
ax.plot(x, y)
ax.set_xlabel(r'x')
ax.set_ylabel(r'\\sin \\left\( x \\right\) ^ 2')
plt.savefig('/path_to_your_project/foo.pgf')

This code reads your tex file (foo.tex), searches for a line containing
\documentclass, searches for the font size (10pt, 9pt, 12pt, whatever) and
uses it as the plot standard font size. If no font size parameter is found in
foo.tex, it defaults to 10pt.

2) since no separate file, with only the text objects (letters, numbers,
labels, annotations, legends, etc) is generated, I am not able to change
them accordingly, later, via Latex itself. That's what gnuplot and inkscape
allow us to do, through the generation of an explicit separate file for the
text objects...

Indeed, you can change the text. It is in the pgf file, inside \pgftext
environments. The example script I've used generates, among other things, the
following line in foo.pgf:

  \pgftext[x=0.072574in,y=1.500000in,bottom,rotate=90.000000]
{{\sffamily\fontsize{10.000000}{12.000000}\selectfont \(\displaystyle \sin
\left( x \right) ^ 2\)}}%

which is the y label.

It is far from ideal, whith its lack of resizing capabilities, but maybe in
future versions the pgf code gets some improvements so it uses tikz and allows
to use things like tikzscale (which, as far as i know, can't be used now to
scale a figure generated by matplotlib).

···

El Lunes, 20 de enero de 2014 09:23:59 Mauricio Calvao escribió:

--
Luis Miguel García-Cuevas González