Ask Your Question
2

Mathematica graphics in a sage notebook

asked 2011-09-21 00:33:54 +0100

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:


%mathematica
Plot[Sin[x],{x,-4,4}]

just produces

     -Graphics-

While

mathematica('Plot[Sin[x],{x,-4,4}]')

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
2

answered 2011-09-21 21:01:51 +0100

updated 2011-09-21 21:05:23 +0100

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/mathematica.py you find the show() function

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

    EXAMPLES::

        sage: P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
        sage: show(P)                                        # optional - mathematica
        sage: P.show(ImageSize=800)                          # 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

Comments

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 16:20:59 +0100 )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 09:56:01 +0100 )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 15:16:22 +0100 )edit

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

enedene gravatar imageenedene ( 2011-10-21 19:05:14 +0100 )edit
2

answered 2011-09-21 09:28:38 +0100

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

Comments

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

Simon gravatar imageSimon ( 2011-09-21 20:59:33 +0100 )edit
1

answered 2011-09-22 18:03:00 +0100

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

Comments

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 21:07:11 +0100 )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

Stats

Asked: 2011-09-21 00:33:54 +0100

Seen: 1,527 times

Last updated: Sep 22 '11