# Mathematica graphics in a sage notebook

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

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 close merge delete

Sort by ยป oldest newest most voted

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.

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.

( 2011-09-22 16:20:59 +0200 )edit

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

( 2011-10-20 09:56:01 +0200 )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!

( 2011-10-20 15:16:22 +0200 )edit

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

( 2011-10-21 19:05:14 +0200 )edit

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.

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!

( 2011-09-25 21:07:11 +0200 )edit

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!

more

( 2011-09-21 20:59:33 +0200 )edit