Ask Your Question

Revision history [back]

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)
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.png", format="png")
plt.close(fig)
\end{sagesilent}

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

\end{document}

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
fft, ifft
N1 = 16
n1 = np.linspace(0.0,16,N1)
np.linspace(0.0, 16, N1)
y = np.array([np.cos(((2.0 * np.pi*i))/N1) np.pi * i))/N1) for i in range (0,N1)])
(0, N1)])
yf = fft(y)
from matplotlib import matplotlib
import matplotlib.pyplot pyplot as plt
fig = plt.figure(2)
ax = fig.add_subplot(221)
plt.plot(n1,y)
ay = plt.plot(n1, y)
fig.add_subplot(222)
p = plt.plot(n1,yf)
g = 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.