Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 primitive