`multiplot` not working in Gnuplot.jl

Extreme beginner in Gnuplot, and trying to put a multiplot together. The code below is directly from the examples from the doc

# Generate random numbers
x = randn(1000);
y = randn(1000);

# Overall plot margins (normalized in the range 0:1)
margins = (l=0.08, r=0.98, b=0.13, t=0.98)

# Right and top margins of main plot
right, top = 0.8, 0.75

# Gap between main plot and histograms
gap  = 0.015

# Main plot
@gp "set multiplot" ## crashes here
# more code #

The error is (with verbosity on).

julia> @gp "set multiplot"
GNUPLOT (default) unset multiplot
GNUPLOT (default) set output
GNUPLOT (default) reset session
GNUPLOT (default) set term unknown
GNUPLOT (default) set multiplot
GNUPLOT (default) 
GNUPLOT (default) reset
GNUPLOT (default) print GPVAL_TERM
GNUPLOT (default) -> unknown
GNUPLOT (default) print GPVAL_TERMOPTIONS
GNUPLOT (default) set term svg enhanced mouse standalone dynamic background rgb 'white'
GNUPLOT (default) -> multiplot> set term svg enhanced mouse standalone dynamic background rgb 'white'
GNUPLOT (default) ->                     ^
GNUPLOT (default) ->            line 6478: You can't change the terminal in multiplot mode
┌ Error: 
│ 
│ multiplot> set term svg enhanced mouse standalone dynamic background rgb 'white'
│                     ^
│            line 6478: You can't change the terminal in multiplot mode
└ @ Gnuplot ~/.julia/packages/Gnuplot/7y0ez/src/Gnuplot.jl:897
GNUPLOT (default) print GPVAL_ERRMSG
Error showing value of type Gnuplot.SessionID:
ERROR: Gnuplot error: ["You can't change the terminal in multiplot mode"]

It seems that the bare @gp macro has some boilerplate code to set up the plot, and then tries to inject set multiplot in there somewhere, causing it to crash as it is not allowed.

Note that I have

julia> Gnuplot.options.gpviewer
false

so that I am able to use the VSCode plot pane.

Any ideas?

Seems that the problem really is with

julia> Gnuplot.options.gpviewer
false

as this is the statement that sets the command set term svg ...

I think that it does not work with the VS Code plot pane in general, but I could be wrong here.

I never tried Gnuplot.jl with VS Code, but I suppose is not too different from Jupyter and Juno. I’ll test again the multiplot capabilities and let you know…

If Gnuplot.options.gpviewer = false then output is a SVG (or png, jpeg), and plots are shown in the vscode plot pane. I am not sure if this is inherent in vscode or part of the package to push the plot to the plot pane display stack.

The way it works is that setting Gnuplot.options.gpviewer = false, the command set term svg ... is run. From my understanding this is setting the output type to a SVG. The problem is that when you are in multiplot mode of gnuplot, it does not allow you to run the set term command.

@gcalderone please let me know if you have a solution.

Alternatively , I can use Gnuplot.options.gpviewer = true (which then uses X11 to push the plots to my local display). In this scenario, multiplot works, but it is extremely slow. It “redraws” the screen everytime it adds a plot to the window.

for i = 1:16
    @gp :-  i x x.^0.5 "w l tit 'Pow 0.5' dt 2 lw 2 lc rgb 'red'" 
end

(it’s also slow beause of X11 forwarding, but I have no other option – unless vscode plot pane works)

@gcalderone According to this, this can be fixed by

just set the term and out variables before you set the multiplot command to get rid of the error message.

does this help?

Try to run the following commands at the beginning of your julia session within VS Code:

using Gnuplot
Gnuplot.options.gpviewer = false
Gnuplot.options.term = ""

You should explicitly set gpviewer since Gnuplot.jl only recognize IJulia (i.e. Jupyter-like) and Juno environments. I have no idea how VS code works, so this is possibly necessary. Setting term may also be required to override any local setting you might have.
It is important to execute this commands at the beginning of your Julia session.

Let me know if this solves the problem, or please open an issue on the Github repo.

Finally, keep in mind that Gnuplot.jl has been designed, and provides the best performances, when using the gnuplot built-in terminals (i.e. by setting gpviewer = true). The multimedia interface (to display on Jupyter/Juno etc.) is still experimental and may be subject to breaking changes in the future…

To speed up your code using the gpviewer = true you may try the followng approach:

for i = 1:16
    @gp :-  i x x.^0.5 "w l tit 'Pow 0.5' dt 2 lw 2 lc rgb 'red'"  :-
end
@gp

Note the trailing :- symbol: it tells Gnuplot.jl to avoid updating the plot at each iteration. The final @gp statement will display the complete plot.

So I tried that

using Gnuplot
Gnuplot.options.gpviewer = false
Gnuplot.options.term = ""
Gnuplot.options.verbose = true

Then I run:

julia> @gp :- "set multiplot layout 4,4 rowsfirst title 'multiplot layout'"
GNUPLOT (default) print GPVAL_TERM
GNUPLOT (default) -> x11
GNUPLOT (default) print GPVAL_TERMOPTIONS
GNUPLOT (default) set multiplot layout 4,4 rowsfirst title 'multiplot layout'
GNUPLOT (default) ->            ^
GNUPLOT (default) ->            line 0: invalid command

So two problems I noticed:

  1. running the multiplot command automatically switches the terminal back to x11 (it actually pops up a empty canvas)
  2. you see that running a multiplot command has a invalid command and the REPL hangs. I have to usually send an interrupt signal (ctrl-C).

I tried setting the term before I run multiplot, but same error

using Gnuplot
Gnuplot.options.gpviewer = false
Gnuplot.options.term = ""
Gnuplot.options.verbose = true

@gp :- "set term svg enhanced mouse standalone dynamic background rgb 'white'" 
@gp :- "set term svg size  600, 400"
@gp :- "set multiplot layout 4,4 rowsfirst title 'multiplot layout'"

GNUPLOT (default) -> You can't change the terminal in multiplot mode
Error showing value of type Gnuplot.SessionID:
ERROR: Gnuplot error: ["You can't change the terminal in multiplot mode"]

And thanks for the tip for the trailing :-, I might just stick with x11 until vscode plot pane is more functional. I will open a github issue to track this and implement it sometime.