Save plots into a multi-page pdf?

asked 2015-11-17 13:24:36 +0200

Philstix gravatar image

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
edit retag flag offensive close merge delete

Comments

I'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.

kcrisman gravatar imagekcrisman ( 2015-11-17 16:01:52 +0200 )edit

Thanks 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.

Philstix gravatar imagePhilstix ( 2015-11-18 02:06:23 +0200 )edit

It may very well work, but again you'd have to convert the Sage figures into matplotlib ones - which is supported.

kcrisman gravatar imagekcrisman ( 2015-11-18 04:42:11 +0200 )edit