Ask Your Question
0

How to set matplotlib backend from SageTeX?

asked 2015-05-17 22:47:57 +0100

lahwaacz gravatar image

The text labels in plots made from SageTeX support some LaTeX formatting, but this is done internally before writing to the output file, even in eps format. Multiple programs for vector graphics (e.g. Inkscape and Gnuplot) support a "eps+latex" format, where the text is left as-is to be typeset when included in a LaTex document. Is something like this possible with Matplotlib, Sage and finally SageTeX?

Since Matplotlib supports the PGF backend, I was thinking about using it with SageTeX to include plots as TikZ figures. Unfortunately I have not figured out a way to do this, the technical problem is that I'm not able to access the matplotlib module from SageTeX to even specify the font size. The solutions suggested in this question does not work for me, the font style and size remain constant (default).

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2015-05-18 08:38:10 +0100

dazedANDconfused gravatar image

updated 2015-05-18 08:41:45 +0100

I'm not really sure I understand the question (why not use LaTeX and sagetex for plotting instead of matplotlib?) but here is an example from the matplotlib site modified to run through LaTeX using sagetex with sizes modified via the solution you pointed out (which didn't work for you):

\documentclass{article}
\usepackage{sagetex}
\usepackage{graphicx}
\pagestyle{empty}
\begin{document}
\begin{sagesilent}
import matplotlib as mpl
mpl.use("pgf")
pgf_with_rc_fonts = {
"font.family": "serif",
"font.serif": [],                   # use latex default serif font
"font.sans-serif": ["DejaVu Sans"], # use a specific sans-serif font
}
mpl.rcParams.update(pgf_with_rc_fonts)

import matplotlib.pyplot as plt
plt.figure(figsize=(4.5,2.5))
plt.rcParams['font.size'] = 6.
plt.plot(range(5))
plt.text(0.5, 3., "serif")
plt.rcParams['font.size'] = 3
plt.text(0.5, 2., "monospace", family="monospace")
plt.rcParams['font.size'] = 24
plt.text(2.5, 2., "sans-serif", family="sans-serif")
plt.rcParams['font.size'] = 16.
plt.text(2.5, 1., "comic sans", family="Comic Sans MS")
plt.rcParams['font.size'] = 8.
plt.xlabel(u"µ is not $\\mu$")
plt.tight_layout(.5)
plt.savefig('MyPic.pdf')
\end{sagesilent}
Sagetex let's you use matplotlib in creating pictures. 
This is a modification of an example comes from:
\begin{verbatim}
http://matplotlib.org/users/pgf.html
\end{verbatim}
and it's running in \LaTeX \,with the power of the sagetex package.
\begin{center}
\includegraphics[width=5in,height=4in]{MyPic.pdf}
\end{center}
\end{document}

When I ran the code in SagemathCloud I didn't get the comic-sans font (perhaps it's not available in SMC?!?) but everything else seems in order and font sizes are modified. I don't see how Tikz is use here.

But the examples shown on that page aren't particularly "LaTeX-like" as the page seems to indicate. If you use LaTeX for the typesetting and Sage for calculations, such as is done for the Cantor function in the answer by DJP then you're getting the LaTeX control over the picture and sagetex is performing the calculations needed for the plot. This solves the problem of being LaTeX friendly while avoiding matplotlib (why is matplotlib so necessary?), giving pictures in tikzpicture environment which look soooo much better.

edit flag offensive delete link more

Comments

Since Matplotlib is used internally by Sage for plotting, I was asking how to configure this instance of Matplotlib. It's a shame if that's not possible.

As for producing "LaTeX-like" plots, I was interested in the PGF output format (i.e. plt.savefig(MyPic.pgf) instead of plt.savefig(MyPic.pdf)), which is then \input in the LaTeX document and plotting is done using the TikZ package. Since this avoids assembling the tikzpicture environment by hand, the motivation for all this is pretty clear I think.

lahwaacz gravatar imagelahwaacz ( 2015-05-18 09:32:36 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-05-17 22:47:57 +0100

Seen: 841 times

Last updated: May 18 '15