Plot directly to file

I am preparing my Julia code as scripts that will run on a remote compute server. When I run the example locally, as julia --project test1.jl, it produces a file sine.pdf. When I run it remotely, it gives the following error:

QXcbConnection: Could not connect to display
Aborted
connect: Connection refused
GKS: can’t connect to GKS socket application
Did you start ‘gksqt’?

GKS: Open failed in routine OPEN_WS
GKS: GKS not in proper state. GKS must be either in the state WSOP or WSAC in routine ACTIVATE_WS

It does, however, also produce the figure sine.pdf.
What should I do to get rid of the error message?

using Plots

x = range(0, 2π, length=300)
y = sin.(x)

fig = plot(x, y);
savefig(fig, "sine.pdf")

Maybe Deactivate plot display to avoid need for X server ?

You can specify the GR output device before starting Julia, e.g.

    export GKS_FILEPATH="sine.pdf"
    export GKS_WSTYPE="pdf"
    julia test1.jl

With this method, you don’t need the savefig() command.

We use a wrapper script to accomplish this:

#!/bin/sh
juliadir=/usr/local/julia

if [ "$1" = "-o" ]
then
  shift
  if [ "`echo $1 | grep '\.'`" != "" ]
  then
    type=`echo $1 | awk -F. '{print $NF}'`
    export GKS_FILEPATH="$1"
    export GKS_WSTYPE="$type"
  else
    export GKS_WSTYPE="$1"
  fi
  shift
fi
exec ${juliadir}/bin/julia "$@"
1 Like