Ask Your Question
1

Displaying a matplotlib picture using SageTeX

asked 2016-12-08 03:45:42 -0500

noufalasharaf gravatar image

updated 2016-12-09 05:49:40 -0500

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 04:35:24 -0500

updated 2016-12-08 04:46:14 -0500

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 11:36:31 -0500 )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-08 21:06:13 -0500 )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 03:45:42 -0500

Seen: 60 times

Last updated: Dec 09 '16