Deactivate plot display to avoid need for X server

I use the following type of code to create a gif animation:

using Plots
anim = Animation();
for it = 1:nt
      contourf(...);
      frame(anim);
end
gif(anim, "test.gif", fps = 15);

This works fine on my local notebook or when I connect to a compute node with X server (ssh -X). However, I need to be able to do that without any X server (for distributed jobs). When I run it without an X server available, I get the following error:

gksqt: cannot connect to X server localhost:12.0
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

Could you tell me how to avoid the need for an X server, i.e. how I can deactivate any display / how I can deactivate GKS?

Thanks!

1 Like

You can set the output type before using the gr backend using ENV["GKSwstype"]=100 or ENV[“GKSwstype”]=“nul”`, which activates the NULL device.

10 Likes

Great setting ENV["GKSwstype"]="nul" makes it work! Thank you very much!

I had a hard time to solve this issue until I found this post. Thank you very much.

At the same time, it would be nice if there was a less hackier way to do it, something like

plot!(display=none)

Don’t you think?

6 Likes

Yes @lmiq, I totally agree: it would be good if one could turn off the display with a function call.

Something close to what you want is this:

using Plots: default
default(show=false)

I learned it from the Plots.jl runtests.jl file:

But I still needed to set ENV as well…

3 Likes

Thanks @JeffFessler for your comment. As you note what actually solves the problem is the environment variable though…

How could you reset ENV[“GKSwstype”] to its default value again after having set ENV[“GKSwstype”]=“nul” in the same julia session?

I ask because I would like to disable the prompting of plots only for a specific part of the code, and then return to the default state.

You can remove or reset the environment variable with delete!(ENV, "GKSwstype") or ENV["GKSwstype"] = "" and then force GR to reload its graphics subsystem using GR.emergencyclosegks() (or Plots.GR.emergencyclosegks() in Plots.)

3 Likes

Just came here to say THANK YOU! I am running scripts on an external server that generate figures saved as pdf. The script prints a log file with diagnostics, and those log files were 95% errors related to GKS and GR from trying to plot the figures on the server. This thread finally solved my problem!

Would you help me.
Why I have the problem.
This code doesn’t work, Plot rise a error (with very long message)

using Plots
ENV["GKSwstype"]="nul"
gr()
x=[1:10]
y=2*x
plot(y,x)

I have the same . did you reach any solution???

it looks like I didn’t use gt().

I’ve check right now. The following code has done perfectly.

using Plots
ENV["GKSwstype"]="nul"
plot(rand(100,1))
Plots.savefig("test.png")
3 Likes

Thanks for the response

Thanks for this useful post. As others above, I believe something as simple as

plot(x, y, file="foo.svg")

(or an explicit display change…) that would override display on stdout would be a welcome addition.

years later I am running into the same problem, but using plotly() backend which tries to open a html plot with xdg-open and fails

ERROR: failed process: Process(`xdg-open /tmp/jl_RGBaJUX69S.html`, ProcessExited(3)) [3]

How can I make silently ignore this?

I now create a dummy xdg-open and put it before the path. Seems to have worked.

Also, suprisingly, when I simply run a julia files myfile.jl which uses normal plot(…) commands using julia myfile.jl, no error is thrown. Looks like Plots already comes with some display logic depending on how it is started