9

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

M.R.
  • 31,425
  • 8
  • 90
  • 281

3 Answers3

10

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.

chuy
  • 11,205
  • 28
  • 48
6

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.

Itai Seggev
  • 14,113
  • 60
  • 84
3

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[];
edgeloss
  • 311
  • 2
  • 6