Saving .show() Output as a Vector Graphic

877 views
Skip to first unread message

Evan Oman

unread,
Feb 23, 2014, 11:22:07 PM2/23/14
to sage-...@googlegroups.com
I am using Sage to make visual representations of Graphs(ie of the vertex and edge variety) via the show command.

However as far as I can tell I can only save these as a raster in the form of a PNG or JPEG? I have been a vector graphic snob as of late and would like to save graphs as PDF like I can regular plots. Is this possible?

Thanks!

PS: I am not sure if this is an appropriate venue to post such a question, if this is the case let me know where I should direct my related inquirers.

P Purkayastha

unread,
Feb 24, 2014, 2:18:00 AM2/24/14
to sage-...@googlegroups.com
On 02/24/2014 12:22 PM, Evan Oman wrote:
> I am using Sage to make visual representations of Graphs(ie of the vertex and edge variety) via the show command.
>
> However as far as I can tell I can only save these as a raster in the form of a PNG or JPEG? I have been a vector graphic snob as of late and would like to save graphs as PDF like I can regular plots. Is this possible?
>
> Thanks!

instead of show() simply use save(). You can give the same arguments to
save() as you give to show(), except for the first one - that should be
the filename, like

plot(x).save('/tmp/a.pdf', <other show options>)
plot(x).save('./a.eps', <other show options>)
plot(x).save('./a.svg', <other show options>)

If the latter two are done in a notebook cell, it will show a link to
the file.

>
> PS: I am not sure if this is an appropriate venue to post such a question, if this is the case let me know where I should direct my related inquirers.
>

You should direct such requests to sage-support.

Evan Oman

unread,
Feb 24, 2014, 4:21:23 PM2/24/14
to sage-...@googlegroups.com
Thanks for the redirect to sage-support! I will be using that for all future questions. 

As for this problem I am using the Graph module which doesn't seem to work with the save command. I tried the following:

graphObject = Graph({1:[2,3,4],2:[1,3,4],3:[1,2,4],4:[1,2,3]})
figure
= graphObject.plot(layout="circular")
figure
.save('./test.pdf')

and I got an AssertionError referring to a bunch of the matplotlib packages.

The only way I have been able to save so far has been through saving the the outputted image via a right click in the browser.

 Is the Graph module incompatible with saving in this manner? 


Jason Grout

unread,
Feb 24, 2014, 11:04:24 PM2/24/14
to sage-...@googlegroups.com
> graphObject =Graph({1:[2,3,4],2:[1,3,4],3:[1,2,4],4:[1,2,3]})
> figure =graphObject.plot(layout="circular")
> figure.save('./test.pdf')
> |
>
> and I got an *AssertionError *referring to a bunch of the matplotlib
> packages.
>
> The only way I have been able to save so far has been through saving the
> the outputted image via a right click in the browser.
>
> Is the Graph module incompatible with saving in this manner?
>

It seems to work for me: http://sagecell.sagemath.org/?q=gpscvj

Thanks,

Jason



Evan Oman

unread,
Feb 25, 2014, 12:49:51 PM2/25/14
to sage-...@googlegroups.com
Interesting, it must have something to do with my sage install on Ubuntu then? Are the packages loaded to the cloud version the same as the standard local install?

I used the Ubuntu PPA to install and the only additional package I installed was Pandas.

Dominique Laurain

unread,
Feb 26, 2014, 2:48:34 PM2/26/14
to sage-...@googlegroups.com
I read, and read again and read again..and don't understand why 1rst parameter "self" is missing in cloud collections.py, with following difference between function definition in backend_pdf.py and call in collections.py  :

    def draw_path_collection(self, gc, master_transform, paths, all_transforms,
                             offsets, offsetTrans, facecolors, edgecolors,
                             linewidths, linestyles, antialiaseds, urls,
                             offset_position):

backend_pdf.py" [readonly] 2312 lines --65%-


       renderer.draw_path_collection(
            gc, transform.frozen(), paths, self.get_transforms(),
            offsets, transOffset, self.get_facecolor(), self.get_edgecolor(),
            self._linewidths, self._linestyles, self._antialiaseds, self._urls,
            self._offset_position)

collections.py" [readonly] 1471 lines --17%--



David Roe

unread,
Feb 26, 2014, 2:51:11 PM2/26/14
to sage-devel


--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
To post to this group, send email to sage-...@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/groups/opt_out.

Dominique Laurain

unread,
Feb 26, 2014, 2:57:46 PM2/26/14
to sage-...@googlegroups.com
OK
Thanks David Roe (I missed the point : within a class)

Evan Oman

unread,
Feb 26, 2014, 4:24:32 PM2/26/14
to sage-...@googlegroups.com
If this helps, running this

graphObject = Graph({1:[2,3,4],2:[1,3,4],3:[1,2,4],4:[1,2,3]})
figure
= graphObject.plot(layout="circular")
figure
.save('./test.pdf')



Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_23.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("Z3JhcGhPYmplY3QgPSBHcmFwaCh7MTpbMiwzLDRdLDI6WzEsMyw0XSwzOlsxLDIsNF0sNDpbMSwyLDNdfSkKZmlndXJlID0gZ3JhcGhPYmplY3QucGxvdChsYXlvdXQ9ImNpcmN1bGFyIikKZmlndXJlLnNhdmUoJy4vdGVzdC5wZGYnKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpUdblPn/___code___.py", line 5, in <module>
    exec compile(u"figure.save('./test.pdf')" + '\n', '', 'single')
  File "", line 1, in <module>
    
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 458, in wrapper
    return func(*args, **kwds)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/plot/graphics.py", line 2732, in save
    transparent=transparent)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/figure.py", line 1370, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2055, in print_figure
    **kwargs)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 1848, in print_pdf
    return pdf.print_pdf(*args, **kwargs)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2301, in print_pdf
    self.figure.draw(renderer)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/figure.py", line 1006, in draw
    func(*args)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/collections.py", line 695, in draw
    return Collection.draw(self, renderer)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/collections.py", line 259, in draw
    self._offset_position)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 1548, in draw_path_collection
    output(*self.gc.pop())
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2093, in pop
    assert self.parent is not None
AssertionError

Any thoughts?

Evan Oman

unread,
Mar 3, 2014, 2:18:57 PM3/3/14
to sage-...@googlegroups.com
So should I use a different function to save on the local version? Is this a bug?
Reply all
Reply to author
Forward
0 new messages