ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 18 May 2015 02:32:36 -0500How to set matplotlib backend from SageTeX?http://ask.sagemath.org/question/26850/how-to-set-matplotlib-backend-from-sagetex/ 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](http://matplotlib.org/users/pgf.html), 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](http://ask.sagemath.org/question/10210/sage-latex-how-to-change-a-font-for-axes-label/) does not work for me, the font style and size remain constant (default).Sun, 17 May 2015 15:47:57 -0500http://ask.sagemath.org/question/26850/how-to-set-matplotlib-backend-from-sagetex/Answer by dazedANDconfused for <p>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?</p>
<p>Since Matplotlib supports the <a href="http://matplotlib.org/users/pgf.html">PGF backend</a>, 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 <a href="http://ask.sagemath.org/question/10210/sage-latex-how-to-change-a-font-for-axes-label/">this question</a> does not work for me, the font style and size remain constant (default).</p>
http://ask.sagemath.org/question/26850/how-to-set-matplotlib-backend-from-sagetex/?answer=26855#post-id-26855I'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](http://matplotlib.org/users/pgf.html) 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](http://tex.stackexchange.com/questions/241622/plotting-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.Mon, 18 May 2015 01:38:10 -0500http://ask.sagemath.org/question/26850/how-to-set-matplotlib-backend-from-sagetex/?answer=26855#post-id-26855Comment by lahwaacz for <p>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 <a href="http://matplotlib.org/users/pgf.html">matplotlib site</a> modified to run through LaTeX using sagetex with sizes modified via the solution you pointed out (which didn't work for you):</p>
<pre><code>\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}
</code></pre>
<p>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.</p>
<p>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 <a href="http://tex.stackexchange.com/questions/241622/plotting-the-cantor-function">Cantor function</a> 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.</p>
http://ask.sagemath.org/question/26850/how-to-set-matplotlib-backend-from-sagetex/?comment=26856#post-id-26856Since 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.Mon, 18 May 2015 02:32:36 -0500http://ask.sagemath.org/question/26850/how-to-set-matplotlib-backend-from-sagetex/?comment=26856#post-id-26856