# Plot directly to file

**URL:** <https://discourse.julialang.org/t/plot-directly-to-file/29716>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [October 10, 2019, 6:46am UTC](https://discourse.julialang.org/t/plot-directly-to-file/29716 "2019-10-10T06:46:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [October 10, 2019, 6:46am UTC](https://discourse.julialang.org/t/plot-directly-to-file/29716/1 "2019-10-10T06:46:40Z")

</div>

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?

```julia
using Plots

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

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

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 10, 2019, 8:27am UTC](https://discourse.julialang.org/t/plot-directly-to-file/29716/2 "2019-10-10T08:27:50Z")

</div>

Maybe [Deactivate plot display to avoid need for X server - #2 by jheinen](https://discourse.julialang.org/t/deactivate-plot-display-to-avoid-need-for-x-server/19359/2) ?

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [October 10, 2019, 8:37am UTC](https://discourse.julialang.org/t/plot-directly-to-file/29716/3 "2019-10-10T08:37:58Z")

</div>

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

```julia
    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:

```julia
#!/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 "$@"

```
