ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 18 Nov 2015 04:42:11 +0100Save plots into a multi-page pdf?https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/ I am creating a series of plots that I would like to save into a multi-page pdf file.
I found a couple of references about using matplotlib in pylab to do so, but I can't get it to work in SageMath. Is it possible to do this in SageMath?
Here's what I tried:
from matplotlib.backends.backend_pdf import PdfPages
pdf_pages = PdfPages('curves.pdf')
p1, p1max, p2, p2max, a = var('p1, p1max, p2, p2max, a')
p1max = 10
p2max = 10
for p1 in [1..p1max]:
for p2 in [1..p2max]:
fig = parametric_plot([cos(a) + cos(p1*a)/2 + sin(p2*a)/3, sin(a) + sin(p1*a)/2 + cos(p2*a)/3], (a, 0, 2 * pi), title = ('(p1, p2) = (' + str(p1) + ', ' + str(p2) + ')'), frame = True, axes_pad = .05)
pdf_pages.savefig(fig)
pdf_pages.close()
... and it produces this error:
File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-linux-x86_64.egg/matplotlib/backends/backend_pdf.py", line 2438, in savefig
raise ValueError("No such figure: " + repr(figure))
ValueError: No such figure: Graphics object consisting of 1 graphics primitiveTue, 17 Nov 2015 13:24:36 +0100https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/Comment by kcrisman for <p>I am creating a series of plots that I would like to save into a multi-page pdf file. </p>
<p>I found a couple of references about using matplotlib in pylab to do so, but I can't get it to work in SageMath. Is it possible to do this in SageMath?</p>
<p>Here's what I tried:</p>
<pre><code>from matplotlib.backends.backend_pdf import PdfPages
pdf_pages = PdfPages('curves.pdf')
p1, p1max, p2, p2max, a = var('p1, p1max, p2, p2max, a')
p1max = 10
p2max = 10
for p1 in [1..p1max]:
for p2 in [1..p2max]:
fig = parametric_plot([cos(a) + cos(p1*a)/2 + sin(p2*a)/3, sin(a) + sin(p1*a)/2 + cos(p2*a)/3], (a, 0, 2 * pi), title = ('(p1, p2) = (' + str(p1) + ', ' + str(p2) + ')'), frame = True, axes_pad = .05)
pdf_pages.savefig(fig)
pdf_pages.close()
</code></pre>
<p>... and it produces this error:</p>
<pre><code>File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-linux-x86_64.egg/matplotlib/backends/backend_pdf.py", line 2438, in savefig
raise ValueError("No such figure: " + repr(figure))
ValueError: No such figure: Graphics object consisting of 1 graphics primitive
</code></pre>
https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/?comment=30788#post-id-30788I'm not really sure that what you are saving is a matplotlib figure, though. You would want to convert the Sage graphics objects to `.matplotlib()` first, I think - and even then I don't know whether Sage is compiled with this backend available, though you should try.Tue, 17 Nov 2015 16:01:52 +0100https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/?comment=30788#post-id-30788Comment by Philstix for <p>I am creating a series of plots that I would like to save into a multi-page pdf file. </p>
<p>I found a couple of references about using matplotlib in pylab to do so, but I can't get it to work in SageMath. Is it possible to do this in SageMath?</p>
<p>Here's what I tried:</p>
<pre><code>from matplotlib.backends.backend_pdf import PdfPages
pdf_pages = PdfPages('curves.pdf')
p1, p1max, p2, p2max, a = var('p1, p1max, p2, p2max, a')
p1max = 10
p2max = 10
for p1 in [1..p1max]:
for p2 in [1..p2max]:
fig = parametric_plot([cos(a) + cos(p1*a)/2 + sin(p2*a)/3, sin(a) + sin(p1*a)/2 + cos(p2*a)/3], (a, 0, 2 * pi), title = ('(p1, p2) = (' + str(p1) + ', ' + str(p2) + ')'), frame = True, axes_pad = .05)
pdf_pages.savefig(fig)
pdf_pages.close()
</code></pre>
<p>... and it produces this error:</p>
<pre><code>File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-linux-x86_64.egg/matplotlib/backends/backend_pdf.py", line 2438, in savefig
raise ValueError("No such figure: " + repr(figure))
ValueError: No such figure: Graphics object consisting of 1 graphics primitive
</code></pre>
https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/?comment=30793#post-id-30793Thanks for replying. Yes, it was an admittedly naive attempt to see if the PdfPages import would work as-is, and I didn't really expect it would. Now I'm hunting around to see if there's any other way of achieving the same result. If not, it's easy enough to create a pdf from individual plot images.Wed, 18 Nov 2015 02:06:23 +0100https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/?comment=30793#post-id-30793Comment by kcrisman for <p>I am creating a series of plots that I would like to save into a multi-page pdf file. </p>
<p>I found a couple of references about using matplotlib in pylab to do so, but I can't get it to work in SageMath. Is it possible to do this in SageMath?</p>
<p>Here's what I tried:</p>
<pre><code>from matplotlib.backends.backend_pdf import PdfPages
pdf_pages = PdfPages('curves.pdf')
p1, p1max, p2, p2max, a = var('p1, p1max, p2, p2max, a')
p1max = 10
p2max = 10
for p1 in [1..p1max]:
for p2 in [1..p2max]:
fig = parametric_plot([cos(a) + cos(p1*a)/2 + sin(p2*a)/3, sin(a) + sin(p1*a)/2 + cos(p2*a)/3], (a, 0, 2 * pi), title = ('(p1, p2) = (' + str(p1) + ', ' + str(p2) + ')'), frame = True, axes_pad = .05)
pdf_pages.savefig(fig)
pdf_pages.close()
</code></pre>
<p>... and it produces this error:</p>
<pre><code>File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib-1.4.3-py2.7-linux-x86_64.egg/matplotlib/backends/backend_pdf.py", line 2438, in savefig
raise ValueError("No such figure: " + repr(figure))
ValueError: No such figure: Graphics object consisting of 1 graphics primitive
</code></pre>
https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/?comment=30795#post-id-30795It may very well work, but again you'd have to convert the Sage figures into matplotlib ones - which is supported.Wed, 18 Nov 2015 04:42:11 +0100https://ask.sagemath.org/question/30786/save-plots-into-a-multi-page-pdf/?comment=30795#post-id-30795