Ask Your Question

Mathematica graphics in a sage notebook

asked 2011-09-20 17:33:54 -0500

Is there an easy way to get Mathematica graphics into a sage notebook?

I have a notebook where I want to compare the Mathematica way of doing things and the related graphics to the sage way and its graphics. The naive ways of doing it do not work:


just produces




returns the InputForm of the graphics, i.e.,

Graphics[{{{}, {}, {Hue[0.67, 0.6, 0.6], Line[{<<long list of points>>}], 
         <<some graphics options>>]

There's three work-arounds I can currently think of.

  1. Export the graphics from Mathematica using: Export["file.png", graphics] and import it back into sage - but I couldn't find an image import function in sage...

  2. Use the JavaGraphics Mathematica package: So

    because code formatting was failing me

    but this puts the graphics object in a separate window (not in the sage notebook).

  3. Extract the graphics data and feed it into a sage plotting function, e.g.

    list_plot(mathematica('Plot[Sin[x], {x, -4, 4}][[1, 1, 3, 2, 1]]').sage(), plotjoined = true)

which does what I want, but is a little cumbersome - especially for more complicated graphics.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2011-09-21 14:01:51 -0500

updated 2011-09-21 14:05:23 -0500

Actually, following kcrisman's hint and looking at the sage-notebook thread, it turns out that it's already implemented and so really easy!

In SAGE_ROOT/devel/sage/sage/interfaces/ you find the show() function

def show(self, filename=None, ImageSize=600):
    Show a mathematica expression or plot in the Sage notebook.


        sage: P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
        sage: show(P)                                        # optional - mathematica
        sage:                          # optional - mathematica
        sage: Q = mathematica('Sin[x Cos[y]]/Sqrt[1-x^2]')   # optional - mathematica
        sage: show(Q)                                        # optional - mathematica
        <html><div class="math">\frac{\sin (x \cos (y))}{\sqrt{1-x^2}}</div></html>

that does exactly what I wanted.

edit flag offensive delete link more


But will they show up without the "show"? Anyway, glad you are happy :) But it would be nice for it to work in the %mathematica mode as well.

kcrisman gravatar imagekcrisman ( 2011-09-22 09:20:59 -0500 )edit

It still doesn't work for me, here's what I get for: mathematica -Graphics- Out[3]= mathematica show -Graphics-

enedene gravatar imageenedene ( 2011-10-20 02:56:01 -0500 )edit

Can you either make a new answer with *exactly* what commands don't work for you, or a new question which references this question? Thanks!

kcrisman gravatar imagekcrisman ( 2011-10-20 08:16:22 -0500 )edit

@kcrisman, I'll ask another question first thing tomorrow. Thank you for your interest.

enedene gravatar imageenedene ( 2011-10-21 12:05:14 -0500 )edit

answered 2011-09-21 02:28:38 -0500

kcrisman gravatar image

You may want to look at how this is implemented for R or other external programs. See this thread for this. You may need to implement several commands in the Mma interface, a chdir or something else; it's different for each program.

Short version: essentially, you need to make sure the output of the program (the graphic file itself) ends up in the correct directory for the cell in the notebook. Good luck!

edit flag offensive delete link more


Thanks kcrisman - the thread you linked to shows that what I want is already implemented! See my answer.

Simon gravatar imageSimon ( 2011-09-21 13:59:33 -0500 )edit

answered 2011-09-22 11:03:00 -0500

Dirk Danckaert gravatar image

I'm not sure this is really relevant, and I don't have any experience with Mathematica worth mentioning, but I just want to address part (1) of your question, about importing images to SAGE. If you use the Notebook interface to sage (in stead of a command shell) it's actually quite simple: open the [Data] dropdown menu above the worksheet and upload an image file to your worksheet. You can then insert the image in a text cell, as an html image.

Sorry if I'm only stating the obvious.

edit flag offensive delete link more


It is relevant and useful. I knew how to add images into a text/html cell, but I had never used the data menu before. Thanks!

Simon gravatar imageSimon ( 2011-09-25 14:07:11 -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


Asked: 2011-09-20 17:33:54 -0500

Seen: 898 times

Last updated: Sep 22 '11