ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 21 Oct 2011 19:05:14 +0200Mathematica graphics in a sage notebookhttps://ask.sagemath.org/question/8339/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
![because code formatting was failing me](http://i.imgur.com/BaPvQ.png)
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.
Wed, 21 Sep 2011 00:33:54 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/Answer by kcrisman for <p><strong>Is there an easy way to get Mathematica graphics into a sage notebook?</strong></p>
<p>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:</p>
<hr/>
<pre><code>%mathematica
Plot[Sin[x],{x,-4,4}]
</code></pre>
<p>just produces</p>
<pre><code> -Graphics-
</code></pre>
<p>While</p>
<pre><code>mathematica('Plot[Sin[x],{x,-4,4}]')
</code></pre>
<p>returns the <code>InputForm</code> of the graphics, i.e.,</p>
<pre><code>Graphics[{{{}, {}, {Hue[0.67, 0.6, 0.6], Line[{<<long list of points>>}],
<<some graphics options>>]
</code></pre>
<hr/>
<p>There's three work-arounds I can currently think of.</p>
<ol>
<li><p>Export the graphics from Mathematica using: <code>Export["file.png", graphics]</code>
and import it back into sage - but I couldn't find an image import function in sage...</p></li>
<li><p>Use the JavaGraphics Mathematica package: So </p>
<p><img alt="because code formatting was failing me" src="http://i.imgur.com/BaPvQ.png"/></p>
<p>but this puts the graphics object in a separate window (not in the sage notebook).</p></li>
<li><p>Extract the graphics data and feed it into a sage plotting function, e.g.</p>
<pre><code>list_plot(mathematica('Plot[Sin[x], {x, -4, 4}][[1, 1, 3, 2, 1]]').sage(), plotjoined = true)
</code></pre></li>
</ol>
<p>which does what I want, but is a little cumbersome - especially for more complicated graphics.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?answer=12673#post-id-12673You may want to look at how this is implemented for R or other external programs. See [this thread](http://groups.google.com/group/sage-notebook/browse_thread/thread/398249cb68c65a5e) 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!Wed, 21 Sep 2011 09:28:38 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?answer=12673#post-id-12673Comment by Simon for <p>You may want to look at how this is implemented for R or other external programs. See <a href="http://groups.google.com/group/sage-notebook/browse_thread/thread/398249cb68c65a5e">this thread</a> for this. You may need to implement several commands in the Mma interface, a chdir or something else; it's different for each program. </p>
<p>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!</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21219#post-id-21219Thanks kcrisman - the thread you linked to shows that what I want is already implemented! See my answer.Wed, 21 Sep 2011 20:59:33 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21219#post-id-21219Answer by Dirk Danckaert for <p><strong>Is there an easy way to get Mathematica graphics into a sage notebook?</strong></p>
<p>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:</p>
<hr/>
<pre><code>%mathematica
Plot[Sin[x],{x,-4,4}]
</code></pre>
<p>just produces</p>
<pre><code> -Graphics-
</code></pre>
<p>While</p>
<pre><code>mathematica('Plot[Sin[x],{x,-4,4}]')
</code></pre>
<p>returns the <code>InputForm</code> of the graphics, i.e.,</p>
<pre><code>Graphics[{{{}, {}, {Hue[0.67, 0.6, 0.6], Line[{<<long list of points>>}],
<<some graphics options>>]
</code></pre>
<hr/>
<p>There's three work-arounds I can currently think of.</p>
<ol>
<li><p>Export the graphics from Mathematica using: <code>Export["file.png", graphics]</code>
and import it back into sage - but I couldn't find an image import function in sage...</p></li>
<li><p>Use the JavaGraphics Mathematica package: So </p>
<p><img alt="because code formatting was failing me" src="http://i.imgur.com/BaPvQ.png"/></p>
<p>but this puts the graphics object in a separate window (not in the sage notebook).</p></li>
<li><p>Extract the graphics data and feed it into a sage plotting function, e.g.</p>
<pre><code>list_plot(mathematica('Plot[Sin[x], {x, -4, 4}][[1, 1, 3, 2, 1]]').sage(), plotjoined = true)
</code></pre></li>
</ol>
<p>which does what I want, but is a little cumbersome - especially for more complicated graphics.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?answer=12681#post-id-12681I'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.Thu, 22 Sep 2011 18:03:00 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?answer=12681#post-id-12681Comment by Simon for <p>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.</p>
<p>Sorry if I'm only stating the obvious.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21202#post-id-21202It 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!Sun, 25 Sep 2011 21:07:11 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21202#post-id-21202Answer by Simon for <p><strong>Is there an easy way to get Mathematica graphics into a sage notebook?</strong></p>
<p>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:</p>
<hr/>
<pre><code>%mathematica
Plot[Sin[x],{x,-4,4}]
</code></pre>
<p>just produces</p>
<pre><code> -Graphics-
</code></pre>
<p>While</p>
<pre><code>mathematica('Plot[Sin[x],{x,-4,4}]')
</code></pre>
<p>returns the <code>InputForm</code> of the graphics, i.e.,</p>
<pre><code>Graphics[{{{}, {}, {Hue[0.67, 0.6, 0.6], Line[{<<long list of points>>}],
<<some graphics options>>]
</code></pre>
<hr/>
<p>There's three work-arounds I can currently think of.</p>
<ol>
<li><p>Export the graphics from Mathematica using: <code>Export["file.png", graphics]</code>
and import it back into sage - but I couldn't find an image import function in sage...</p></li>
<li><p>Use the JavaGraphics Mathematica package: So </p>
<p><img alt="because code formatting was failing me" src="http://i.imgur.com/BaPvQ.png"/></p>
<p>but this puts the graphics object in a separate window (not in the sage notebook).</p></li>
<li><p>Extract the graphics data and feed it into a sage plotting function, e.g.</p>
<pre><code>list_plot(mathematica('Plot[Sin[x], {x, -4, 4}][[1, 1, 3, 2, 1]]').sage(), plotjoined = true)
</code></pre></li>
</ol>
<p>which does what I want, but is a little cumbersome - especially for more complicated graphics.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?answer=12678#post-id-12678Actually, following kcrisman's hint and looking at the [sage-notebook thread](https://groups.google.com/d/msg/sage-notebook/OYJJy2jGWl4/FINW_xdI6tgJ), it turns out that it's already implemented and so really easy!
In [SAGE_ROOT/devel/sage/sage/interfaces/mathematica.py](http://hg.sagemath.org/sage-main/file/2a2abbcad325/sage/interfaces/mathematica.py#l824) 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.
Wed, 21 Sep 2011 21:01:51 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?answer=12678#post-id-12678Comment by kcrisman for <p>Actually, following kcrisman's hint and looking at the <a href="https://groups.google.com/d/msg/sage-notebook/OYJJy2jGWl4/FINW_xdI6tgJ">sage-notebook thread</a>, it turns out that it's already implemented and so really easy!</p>
<p>In <a href="http://hg.sagemath.org/sage-main/file/2a2abbcad325/sage/interfaces/mathematica.py#l824">SAGE_ROOT/devel/sage/sage/interfaces/mathematica.py</a> you find the <code>show()</code> function</p>
<pre><code>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>
"""
</code></pre>
<p>that does exactly what I wanted.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21076#post-id-21076Can you either make a new answer with *exactly* what commands don't work for you, or a new question which references this question? Thanks!Thu, 20 Oct 2011 15:16:22 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21076#post-id-21076Comment by enedene for <p>Actually, following kcrisman's hint and looking at the <a href="https://groups.google.com/d/msg/sage-notebook/OYJJy2jGWl4/FINW_xdI6tgJ">sage-notebook thread</a>, it turns out that it's already implemented and so really easy!</p>
<p>In <a href="http://hg.sagemath.org/sage-main/file/2a2abbcad325/sage/interfaces/mathematica.py#l824">SAGE_ROOT/devel/sage/sage/interfaces/mathematica.py</a> you find the <code>show()</code> function</p>
<pre><code>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>
"""
</code></pre>
<p>that does exactly what I wanted.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21067#post-id-21067@kcrisman, I'll ask another question first thing tomorrow. Thank you for your interest.Fri, 21 Oct 2011 19:05:14 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21067#post-id-21067Comment by enedene for <p>Actually, following kcrisman's hint and looking at the <a href="https://groups.google.com/d/msg/sage-notebook/OYJJy2jGWl4/FINW_xdI6tgJ">sage-notebook thread</a>, it turns out that it's already implemented and so really easy!</p>
<p>In <a href="http://hg.sagemath.org/sage-main/file/2a2abbcad325/sage/interfaces/mathematica.py#l824">SAGE_ROOT/devel/sage/sage/interfaces/mathematica.py</a> you find the <code>show()</code> function</p>
<pre><code>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>
"""
</code></pre>
<p>that does exactly what I wanted.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21079#post-id-21079It still doesn't work for me, here's what I get for: mathematica -Graphics-
Out[3]= mathematica show -Graphics-
Thu, 20 Oct 2011 09:56:01 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21079#post-id-21079Comment by kcrisman for <p>Actually, following kcrisman's hint and looking at the <a href="https://groups.google.com/d/msg/sage-notebook/OYJJy2jGWl4/FINW_xdI6tgJ">sage-notebook thread</a>, it turns out that it's already implemented and so really easy!</p>
<p>In <a href="http://hg.sagemath.org/sage-main/file/2a2abbcad325/sage/interfaces/mathematica.py#l824">SAGE_ROOT/devel/sage/sage/interfaces/mathematica.py</a> you find the <code>show()</code> function</p>
<pre><code>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>
"""
</code></pre>
<p>that does exactly what I wanted.</p>
https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21216#post-id-21216But 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.Thu, 22 Sep 2011 16:20:59 +0200https://ask.sagemath.org/question/8339/mathematica-graphics-in-a-sage-notebook/?comment=21216#post-id-21216