[ANN]: Gnuplot v1.1.0 - A Julia interface to gnuplot

Thanks! That works great!

Maybe this sort of goes against the whole idea but have you considered creating a second package, maybe GnuplotSimple.jl, that wraps the necessary functionality to make basic plotting easier? I am starting to get the hang of all of this, and I’m sure there are shortcuts I don’t yet know about, but it does seem a bit excessive in terms of LOC to make even the easiest of line plots.

Something like

function gnuplot(x,Y; title="", xlab="", ylab="", labels="", grid=true, xr=nothing, yr=nothing, save=false)
# all the @gp stuff
# save commands if asked
end

for the really basic functionality?

Anyways, this is likely my main plotting package for now.

One of the main driving idea of Gnuplot.jl is indeed to save typing as much as possible, and you can already pass x, y and many of the keywords you mentioned directly to @gp, e.g.:

@gp x y
@gp x y "w l"
@gp xlab="X" x y
@gp xr=[1,5] x y

I’m afraid that trying to be even more concise, may only result in a loss of generality…

In your case, no keyword is foreseen for terminal options and output file names, hence I guess you want to hardcode them in the function body to save typing. This is perfectly fine, but this would hardly be a general solution…

I understand. So maybe you could provide me with a hint then. Suppose I am plotting 3 lines, each against the same x points, and I want to assign a label to each line. Let’s also say I want to plot from [0,1] only, and label each axis and the plot itself.

Do I need to write out

# setup
x = 0:0.05:2
y = x.^(1:3)'
# I won't count these lines against you :)

@gp "set grid"
@gp :- tit="The Example" xlab="My x" ylab="The y value" xr=[0,1]
@gp :- x y[:,1] "w l tit 'y1'"
@gp :- x y[:,2] "w l tit 'y2'"
@gp :- x y[:,3] "w l tit 'y3'"

or is there a shorter way?

Also, to be clear, my suggestion dealt with cases like this example. Certainly, you could not wrap all functionality in a plot function like that. But it is not often that you need to rapidly build plots that use all of the sophisticated features.

Yes, a shorter way (but hopefully still readable…) is:

@gp "set grid" ti="The Example" xla="My x" yla="The y value" xr=[0,1]
@gp :- x y[:,1] "w l t 'y1'" x y[:,2] "w l t 'y2'" x y[:,3] "w l t 'y3'"

Also, if you’re simply exploring data and don’t care about aesthetic details, you may simply type:

@gp x y[:,1] "w l" x y[:,2] "w l" x y[:,3] "w l"
1 Like

Forgive me if I don’t find that vastly better than what I wrote :wink:

I think I’ll probably just write it up myself

1 Like

Yes, of course!

Just consider that I don’t think you could get anything shorter with other plotting packages… :wink:

I started it here: GitHub - tbeason/GnuplotSimple.jl: A simple plotting recipe for using Gnuplot.jl

Have not done extensive testing, but it does work well enough for the stuff I’ve tried so far.

I stumbled upon your recipe example and thought it would work well if I built it off of that, so I did.

OK, now I got it! You want shortcut to plot the columns of the Y matrix against the x values.

Here’s how I would do it:

using Gnuplot

# Define an explicit recipe for this particular problem
function multiY(x::AbstractVector{Float64}, Y::AbstractMatrix{Float64}, labels=Vector{String})
    out = Vector{Gnuplot.PlotElement}()
    for i in 1:size(Y)[2]
        plotspec = "w l t '$(labels[i])'"
        push!(out, Gnuplot.PlotElement(data=Gnuplot.Dataset([x, Y[:, i]]), plot=plotspec))
    end
    return out
end

x = 0:0.05:2
y = x.^(1:3)'

@gp "set grid" tit="The Example" xlab="My x" ylab="The y value" xr=[0,1]
@gp :- multiY(x, y, ["y1", "y2", "y3"])
2 Likes

Yea! That is pretty much what I did, but I just fancied it up a bit, so that I can include everything in that single call. Feel free to steal it and improve it lol

1 Like

Hello,
I did some recipes and uploaded some examples at Plotting Market Data from AlphaVantage.jl with Gnuplot.jl - Nextjournal. Take a look.

6 Likes

Dear gcalderone,
I am struggling with gnuplot.jl and dates. Copying your example code throws:
ERROR: LoadError: AssertionError: Invalid argument at position 1.
Are there any changes I am not aware of? (I am on windows, if this matters at all).
Thank you in advance

which example? Could you copy paste here? or a link…

He means the example of @gcalderone which he replied to:

using Gnuplot, Dates

dates = [ Date(2020, 4, floor.(Int, x .* 30 .+ 1)) for x in rand(10) ]
val=rand(10)

@gp "set xdata time" "set timefmt '\"%Y-%m-%d\"'" :-
@gp :- "set format x '%Y-%m-%d'" "set xtics rotate by -45" :-
@gp :- "set rmargin 7" :-
@gp :- string.(dates) val "u 1:2 w p notitl"

I can confirm, i get the same error.

The @gp makro here doesn’t accept an array of strings. This is the code, which leads to the error:

       d = args[iarg]

        ok = false
        if typeof(d) <: Number
            ok = true
        elseif typeof(d) <: AbstractArray
            if typeof(d[1]) <: Number
                ok = true
            end
            if typeof(d[1]) <: ColorTypes.RGB
                ok = true
            end
        end
        if ndims(d) > maxDim
            maxDim = ndims(d)
        end

        @assert ok "Invalid argument at position $iarg"

in src\Gnuplot.jl line 294.

Yes, this example.
It would be a smart idea to add a working example involving dates to your list of examples here, https://lazarusa.github.io/gnuplot-examples/,
which is a perfect reference for gp.

In the mean time I was trying to produce a plot (Windows 10, J v1.5.3, GP v1.0.0) but it doesn’t work.

I stop now because after reading
https://gcalderone.github.io/Gnuplot.jl/v1.3.0/options/#Display-options-1

and finding that Gnuplot.options doesn’t exist, I give up.

There is either something trivial missing, which one needs to know, or there is a major issue with v1.0.0 and Windows. Let’s see if @gcalderone can give us a hint.


Works with

using Gnuplot, Dates

dates = [ Date(2020, 4, floor.(Int, x .* 30 .+ 1)) for x in rand(10) ]
val=rand(10)

@gp "set xdata time" :-
@gp "set timefmt '\"%Y-%m-%d\"'" :-
@gp :- "set format x '%Y-%m-%d'" "set xtics rotate by -45" :-
@gp :- "set rmargin 7" :-
@gp :- string.(dates) val "u 1:2 w p notitl"

Julia 1.6 on Windows 10.

Edit: better esthetics.

using Gnuplot, Dates

dates = [ Date(2020, 4, floor.(Int, x .* 30 .+ 1)) for x in rand(10) ]
val=rand(10)

@gp "set xdata time" :-
@gp :- "set timefmt '\"%Y-%m-%d\"'" :-
@gp :- "set format x '%Y-%m-%d'" "set xtics rotate by -45" :-
@gp :- "set rmargin 7" :-
@gp :- "set style line 1 lc rgb 'blue' lt 1 lw 8 pt 7 ps 2.0" :-
@gp :- string.(dates) val "u 1:2 ls 1 notitl"
2 Likes

ok, It’s not easy to give up, so I tried further.

My conclusion is, install Gnuplot.jl v1.3.0:

(@v1.6) pkg> add Gnuplot@1.3.0

I had issues with Julia 1.5.3, so I tried with 1.6 and voila, it works now as in the example.

(Ninja’d by @PetrKryslUCSD ) :slight_smile: