using PyPlot
##### plot appearance setup
linewidth = 0.4
plt.rc("axes", linewidth=linewidth)
plt.rc("font", family="")
plt.rc("axes", titlesize="small", labelsize="small")
plt.rc("xtick", labelsize="x-small")
plt.rc("xtick.major", width=linewidth/2)
plt.rc("ytick", labelsize="x-small")
plt.rc("ytick.major", width=linewidth/2)
plt.rc("legend", fontsize="small")
#### plot
x = linspace(0,2pi,100);
fig = plt.figure(figsize=(3,2));
plot(x,sin(x),color="red");
title(L"Test plot");
xlabel(L"$x$");
ylabel(L"$\sin(x)$");
#### save plot as PGF source (no setup needed)
plt.savefig("test.pgf")
### setup matplotlib to save PDF with the PGF backend (tricky in Julia)
using PyCall
backend_pgf = pyimport("matplotlib.backends.backend_pgf")
backend_bases = pyimport("matplotlib.backend_bases")
backend_bases[:register_backend]("pdf", backend_pgf[:FigureCanvasPgf])
### setup PGF-PDF backend output appearance
plt.rc("pgf", texsystem="pdflatex",
preamble=L"""\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}""")
#### save directly as PDF (there is NO need to save as PFG first)
plt.savefig("test.pdf", transparent=true)It took me long time to figure out how to properly tell matplotlib (from within Julia) to use that backend tosave PDF (I had to use PyCall in the end), but since then I've been successfully using this feature in Julia.
matplotlib.use('pgf')from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)matplotlib[:use]("pgf")UserWarning: This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.println(PyPlot.matplotlib[:rcParams]["axes.linewidth"])
PyPlot.matplotlib[:rcParams]["axes.linewidth"] = 2 # this does not work
println(PyPlot.matplotlib[:rcParams]["axes.linewidth"])
plt.rc("axes", linewidth=2) # but this works
println(PyPlot.matplotlib[:rcParams]["axes.linewidth"])
1.0
1.0
2.0from matplotlib.backends.backend_pgf import FigureCanvasPgf
matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)
Were you aware that matplotlib itself has a PGF/TikZ backend http://matplotlib.org/users/pgf.html ?
An example of how to save a figure in PGF source:using PyPlot
##### plot appearance setup
linewidth = 0.4
plt.rc("axes", linewidth=linewidth)
plt.rc("font", family="")
plt.rc("axes", titlesize="small", labelsize="small")
plt.rc("xtick", labelsize="x-small")
plt.rc("xtick.major", width=linewidth/2)
plt.rc("ytick", labelsize="x-small")
plt.rc("ytick.major", width=linewidth/2)
plt.rc("legend", fontsize="small")
#### plot
x = linspace(0,2pi,100);
fig = plt.figure(figsize=(3,2));
plot(x,sin(x),color="red");
title(L"Test plot");
xlabel(L"$x$");
ylabel(L"$\sin(x)$");
#### save plot as PGF source (no setup needed)
plt.savefig("test.pgf")
If one wants to directly generate a PDF, then### setup matplotlib to save PDF with the PGF backend (tricky in Julia)
using PyCall
backend_pgf = pyimport("matplotlib.backends.backend_pgf")
backend_bases = pyimport("matplotlib.backend_bases")
backend_bases[:register_backend]("pdf", backend_pgf[:FigureCanvasPgf])
### setup PGF-PDF backend output appearance
plt.rc("pgf", texsystem="pdflatex",
preamble=L"""\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}""")
#### save directly as PDF (there is NO need to save as PFG first)
plt.savefig("test.pdf", transparent=true)I think you can make your package to use this features.