9
$\begingroup$

I'm running mathematica in command line mode, but I need to view a graph/image, so I'd like to open an image with an X11 window to inspect an image:

enter image description here

Is there an easy way or built-in command to to this?

Update:

This differs from the post Mr. Wizard references, because the terminal is running remotely in ssh:

enter image description here

This is how I do it in the terminal usually:

enter image description here

but doesn't work from within mathematica:

enter image description here

$\endgroup$
9
  • 1
    $\begingroup$ What does $DisplayFunction return on the command line? $\endgroup$ Jul 26, 2016 at 16:44
  • $\begingroup$ One easy way to do it is to export the file, then have the system open it. i.stack.imgur.com/Okn2s.png $\endgroup$
    – Jason B.
    Jul 26, 2016 at 16:50
  • $\begingroup$ Would you please clarify how this differs from (48239)? $\endgroup$
    – Mr.Wizard
    Jul 26, 2016 at 17:24
  • $\begingroup$ $DisplayFunction is Identity $\endgroup$
    – M.R.
    Jul 26, 2016 at 17:38
  • 1
    $\begingroup$ Then you can modify it so that graphical output is redirected to e.g. stdout. What OS are you on? $\endgroup$ Jul 26, 2016 at 17:40

3 Answers 3

10
$\begingroup$

Might be relevant, but this is something I use that uses StartProcess and ProcessConnection.

xShow[expr_] := Module[{pr = StartProcess[{"display", "png:fd:0"}]},
  WriteLine[ProcessConnection[pr, "StandardInput"], 
    ExportString[expr, "PNG"]];
  ]

then

Plot3D[Sinh[x]Sinc[y],{x,0,Pi},{y,0,Pi}]//xShow

results in:

Screenshot

This pipes the output to the program display.

I wanted to use this as the $DisplayFunction, but that doesn't cover things like GeoGraphics or other functions that don't use $DisplayFunction, and Plot does some strange doubling (you first get a blank plot and then a plot with the function).

A clever use of $Post could be used rather than my Postfix use.

$\endgroup$
6
$\begingroup$

As long you've set X11 forwarding correctly JavaGraphics should work just fine. I.e, ssh -Y remote followed by

<<JavaGraphics`
Plot3D[Sin[x],{x,0,2Pi},{y,0,1}]

in the remote kernel.

$\endgroup$
3
$\begingroup$

I always use Mathematica from the command line and have usually done display by setting the file init.m in Library/Mathematica/...Kernel/ to the script below. This works on a Mac; it writes the graphics to a pdf file, then displays it.

Unfortunately, in Math 11 some of the graphics displays no longer use $DisplayFunction (e.g. ListPlot, Show, ...) and I haven't found a good workaround yet. But the code below works well for Plot, Plot3D, etc.

(*-----------Graphics for command line Mathematica ----------------*)
(*  Install this file in Math../Configuration/Kernel/init.m *)
(*  It may be necessary to create this directory *)
(*  Writes images as PDF files to /tmp/math.xx; then uses Preview to show them *)

Begin["System`Private`"]  ;
MaxImages = 100;
ImageNumber = 0;
Unprotect[ $DisplayFunction]; Clear[ $DisplayFunction]; $Display = {};
$DisplayFunction := Block[{fid,str},
fid="/tmp/math."<>ToString[ImageNumber]<>".pdf";
str=Export[fid, #,"pdf"];
Run["open -a Preview "<>fid<>" &"];
ImageNumber = Mod[ImageNumber+1,MaxImages];
        #]&;
End[];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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