Adam Webb <adamwebb_rrs <at> yahoo.com> writes:
> 
> Hi,
> 
> I would like to add plots to a document using sagetex and my script
> uses matplotlib to generate the plot. Is it possible to matplotlib
> with sagetex? I understand that /sageplot{} wants a <graphics obj>
> with a .save method. However, I have not found how to generate such an
> object.
> 
> For example if I use the simple example:
>     sage: from pylab import *
>     sage: t = arange(0.0, 2.0, 0.01)
>     sage: s = sin(2*pi*t)
>     sage: P = plot(t, s, linewidth=1.0)
>     sage: xl = xlabel('time (s)')
>     sage: yl = ylabel('voltage (mV)')
>     sage: t = title('About as simple as it gets, folks')
>     sage: grid(True)
>     sage: savefig('sage.png')
> 
> Where is the <graphics obj>?
> 
> cheers,
> Adam
> 


Hi,
i know that it's been a while since this question was asked, but since i 
struggled with it a bit (and to have a reference for later), here's the 
workaround i've come up with:

1. (assuming that this is the file that produces the Matplotlib figure, eg. 
$HOME/sagetexInclude.py):

def f():
    from numpy import arange
    from matplotlib.pyplot import plot
    
    plot(range(10), arange(1.5, 2.5, .1))



2. tweek the file $SAGE_ROOT/local/bin/sage-env (so that is uses PYTHONPATH)*:
" ...
PYTHONPATH="$SAGE_PATH:$SAGE_ROOT/local/lib/python:$PYTHONPATH" && export 
PYTHONPATH
 ...
"
[*Note: now i see that probably setting SAGE_PATH to the appropriate value 
would suffice]


3. in the .tex file do something like:
\begin{sagesilent}
import sagetexInclude as sti
import matplotlib.pyplot as plt
f = plt.figure()  # so that we get a separate image for the plot
sti.f()           # here we do the PLOTting
plt.save = plt.savefig  # SageTeX's plotting apparently relies on *this*!
\end{sagesilent}

\begin{center} \sageplot{plt} \end{center}  % plot as a normal Sage object

% this is not really necessary, but whatever
\begin{sagesilent} plt.close() \end{sagesilent}


4. generate latex and run sage on the file as usually (don't forget to set 
PYTHONPATH or SAGE_PATH to the directory containing sagetexInclude [i.e. export 
PYTHONPATH=$HOME:$PYTHONPATH]):
pdflatex texfile.tex
sage texfile.sage
pdflatex texfile.tex


Hope this helps [someone :) ].

Best regards,
Constantin


-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to