Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Being a long time unix shell person, I prefer Mathematica's command line interface, over the notebook. (command history, etc...)

It just learned that on OS X, the cli is available as MathKernel.

What I'm wondering now is, is it possible to open graphics (and/or animations, sounds, etc) result, from within the command line?

In[16]:= Plot[Sin[x], {x, -Pi, Pi}]

Out[16]= -Graphics-

I would like to be able to view output #16.

share|improve this question
You need to load <<JavaGraphics` . See also my answer here (that question and title should probably rewritten) – R. M. May 21 '14 at 4:20
@rm-rf Strangely, when using <<Terminal` , I get garbage output on OS X. – Szabolcs May 21 '14 at 14:03
@Szabolcs Terminal is for ASCII graphics and it has worked for me – R. M. May 21 '14 at 14:06
@rm-rf I thought it has worked for me too, but I'm getting this now. Have you tried on Mavericks? – Szabolcs May 21 '14 at 14:14
@Szabolcs You're right, that's what I get too on Mavericks. Nothing comes to mind immediately as to what could be wrong... – R. M. May 21 '14 at 15:37
up vote 2 down vote accepted

You must use a FrontEnd to generate actual images, but you don't have to use the notebook interface. You can actually do all of this from a standalone kernel. (You must be logged in to a system that allows you to launch GUI applications, though.)

For example:

In[5]:= g=Plot[Sin[x],{x,-Pi,Pi}];                                              

In[6]:= Export["testplot.png",g]                                                

Out[6]= testplot.png

In[7]:= UsingFrontEnd[SystemOpen["testplot.png"]]

You first create the object you want to see (in this case expression g). Then you pass it to Export. Export will launch the FrontEnd as a service to render the expression, and then the Kernel will convert it to the format specified in Export and save it to disk. You can then view it using SystemOpen. This technique will also work with animations (you will need to use an export format like QuickTime).

share|improve this answer
Thanks to everyone who answered my question. This, and the info about <<JavaGraphics` and <<Terminal` are exactly what I am looking for. (fwiw, I also get noise with terminal graphics under Mavericks, but I feel like that can probably be sorted out.) – billc May 21 '14 at 17:00
@ChristopherCole Just curious: why does the kernel have to launch the FrontEnd to export? (I assume that's just images?) It seems odd that if we are writing data to a file that the kernel couldn't do that by itself. – mfvonh May 21 '14 at 17:32
Ultimately, you need something to render Graphics[...directives, primitives, and options...]. The FrontEnd isn't the only program that knows how to do this; JavaGraphics` in the Kernel can render those primitives and directives it understands. However, since V6, Graphics, like formatted text and equations, are really box expressions and therefore subject to styles and style inheritance. The FrontEnd knows how to resolve these styles, so asking the FrontEnd to perform the render means that the exported graphic will have nearly the same appearance as in a Notebook. – Christopher Cole May 21 '14 at 18:42

If you are using a windowing system, the following before evaluating a graphic should work.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.