Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
1

Displaying a matplotlib picture using SageTeX

asked 8 years ago

noufalasharaf gravatar image

updated 8 years ago

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}

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 8 years ago

slelievre gravatar image

updated 8 years ago

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.

Preview: (hide)
link

Comments

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

noufalasharaf gravatar imagenoufalasharaf ( 8 years ago )
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 ( 8 years ago )

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: 8 years ago

Seen: 453 times

Last updated: Dec 09 '16