Ask Your Question
1

Displaying a matplotlib picture using SageTeX

asked 2016-12-08 10:45:42 +0100

noufalasharaf gravatar image

updated 2016-12-09 12:49:40 +0100

slelievre gravatar image

I couldn't draw the picture I want in a LaTeX document using SageTeX.

Here is my code.

\begin{sagesilent} import numpy as np from scipy.fftpack import fft,ifft N1 = 16 n1 = np.linspace(0.0,16,N1) y = np.array([np.cos(((2.0 * np.pi*i))/N1) for i in range (0,N1)]) yf = fft(y) import matplotlib import matplotlib.pyplot as plt fig = plt.figure(2) ax = fig.add_subplot(221) plt.plot(n1,y) ay = fig.add_subplot(222) p = plt.plot(n1,yf) g = plt.savefig("test.svg", format="svg") plt.close(fig) \end{sagesilent}

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-12-08 11:35:24 +0100

slelievre gravatar image

updated 2016-12-08 11:46:14 +0100

In the code you provide, the figure is saved in svg format.

Instead, you might want to save it to png format and then include it in your latex document.

The following works for me.

\documentclass[a4paper]{article}
\usepackage{sagetex}
\usepackage{graphicx}
\begin{document}%
\pagestyle{empty}

\begin{sagesilent}
import numpy as np
from scipy.fftpack import fft, ifft
N1 = 16
n1 = np.linspace(0.0, 16, N1)
y = np.array([np.cos(((2.0 * np.pi * i))/N1) for i in range (0, N1)])
yf = fft(y)
from matplotlib import pyplot as plt
fig = plt.figure(2)
fig.add_subplot(221)
plt.plot(n1, y)
fig.add_subplot(222)
plt.plot(n1, yf)
plt.savefig("test.png", format="png")
plt.close(fig)
\end{sagesilent}

\includegraphics[scale=0.6]{test.png}

\end{document}

The SageTeX manual and the SageTeX example file show how to use the \sageplot command to include Sage plots in a TeX document.

Here, you are using matplotlib, and saving a picture, so you need to save it in a format that can be included in a LaTeX document, and to actually include it.

edit flag offensive delete link more

Comments

Thank you slelievre. I too get out of the issue...

noufalasharaf gravatar imagenoufalasharaf ( 2016-12-08 18:36:31 +0100 )edit
1

If this answers the question, please feel free to click the check mark so that others will know it has been answered!

kcrisman gravatar imagekcrisman ( 2016-12-09 04:06:13 +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

Stats

Asked: 2016-12-08 10:45:42 +0100

Seen: 413 times

Last updated: Dec 09 '16