Minor changes

This commit is contained in:
Giorgio Calderone 2020-03-16 20:28:44 +01:00
parent a655e5af6c
commit e7f500b5a0
3 changed files with 57 additions and 59 deletions

View File

@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/gcalderone/Gnuplot.jl.svg?branch=master)](https://travis-ci.org/gcalderone/Gnuplot.jl)
**Gnuplot.jl** allows easy and fast use of [Gnuplot](http://gnuplot.info/) as data visualization tool in Julia. Its main features are:
**Gnuplot.jl** allows easy and fast use of [Gnuplot](http://gnuplot.info/) as a data visualization tool in Julia. Its main features are:
- transparent interface between Julia and gnuplot to exploit all functionalities of the latter, both present and future ones;
@ -19,7 +19,6 @@
- very easy to use: if you know gnuplot you're ready to go.
The purpose is similar to the [Gaston](https://github.com/mbaz/Gaston.jl) package, but **Gnuplot.jl** main focus is on on the syntax conciseness and ease of use.
@ -27,17 +26,16 @@ The purpose is similar to the [Gaston](https://github.com/mbaz/Gaston.jl) packag
In the Julia REPL type:
``` julia
using Pkg
Pkg.add("Gnuplot")
```
You'll also need [gnuplot](http://gnuplot.info/) (ver. >= 4.7) installed on your system.
You'll also need [gnuplot](http://gnuplot.info/) (ver. >= 4.7) installed on your system and its executable available in your path.
## Usage:
The simplemost plot ever can be generated with just 8 characters:
The simplemost plot ever can be generated with just 7 characters:
``` Julia
using Gnuplot
@gp 1:10
@gp 1:9
```
A slightly more complicated one showing a parabola with a solid line and a title:
@ -47,7 +45,6 @@ x = 1:10
```
A real life example showing some random noise generated data:
``` Julia
# Create some noisy data...
x = range(-2pi, stop=2pi, length=100);
@ -59,56 +56,58 @@ e = 0.5 * fill(1., length(x));
@gp("set key horizontal", "set grid", title="My title",
xrange=(-7,7), ylabel="Y label", xlab="X label",
x, y, "w l t 'Real model' dt 2 lw 2 lc rgb 'red'",
x, y+noise, e, "w errorbars t 'Data'");
x, y+noise, e, "w errorbars t 'Data'")
```
That's it for the first plots. The syntax should be familiar to most gnuplot users, with this code we:
The syntax should be familiar to most gnuplot users, with the code above we:
- set a few gnuplot properties (`key` and `grid`);
- set the X axis range and Y axis label;
- send the data to gnuplot;
- plot two data sets specifying a few details (style, line width, color, legend, etc...).
Note that this simple example already covers the vast majority of use cases, since the remaining details of the plot can be easily tweaked by adding the appropriate gnuplot command. Also note that you would barely recognize the Julia language by just looking at the `@gp` call since **Gnuplot.jl** aims to be mostly transparent: the user is supposed to focus only on the data and on the gnuplot commands, rather than the package details.
Note that this simple example already covers the vast majority of use cases, since the remaining details of the plot can be easily tweaked by adding the appropriate gnuplot command. Also note that you would barely recognize the Julia language by just looking at the `@gp` call since **Gnuplot.jl** aims to be mostly transparent: the user is supposed to focus only on the data and on the gnuplot commands, rather than the package interface.
If you set the verbose option (`setverbosity(true)`, which is `false` by default) you'll be able to see all the communication taking place between the **Gnuplot.jl** package and the underlyng Gnuplot process. Repeating the last command:
If you set the verbose option (`Gnuplot.setverbose(true)`, which is `false` by default) you'll be able to see all the communication taking place between the **Gnuplot.jl** package and the underlyng Gnuplot process. Repeating the last command:
```Julia
julia> @gp("set key horizontal", "set grid", title="My title",
xrange=(-7,7), ylabel="Y label", xlab="X label",
x, y, "w l t 'Real model' dt 2 lw 2 lc rgb 'red'",
x, y+noise, e, "w errorbars t 'Data'");
GNUPLOT (default) reset session
x, y+noise, e, "w errorbars t 'Data'")
GNUPLOT (default) print GPVAL_TERM
GNUPLOT (default) -> qt
GNUPLOT (default) print GPVAL_TERMOPTIONS
GNUPLOT (default) -> 0 title "Gnuplot.jl: default" font "Sans,9"
GNUPLOT (default) -> 0 font "Sans,9"
GNUPLOT (default) set term qt 0 font "Sans,9" title 'Gnuplot.jl: default'
GNUPLOT (default) reset session
GNUPLOT (default) set key horizontal
GNUPLOT (default) set grid
GNUPLOT (default) set title 'My title'
GNUPLOT (default) set title "My title"
GNUPLOT (default) set xrange [-7:7]
GNUPLOT (default) set ylabel 'Y label'
GNUPLOT (default) set xlabel 'X label'
GNUPLOT (default) set ylabel "Y label"
GNUPLOT (default) set xlabel "X label"
GNUPLOT (default) $data0 << EOD
GNUPLOT (default) -6.283185307179586 1.2258873407968363
GNUPLOT (default) -6.156252270670907 1.1443471266509504
GNUPLOT (default) -6.029319234162229 1.05377837392046
GNUPLOT (default) -5.90238619765355 0.9548956415530343
GNUPLOT (default) ...
GNUPLOT (default) EOD
GNUPLOT (default) $data1 << EOD
GNUPLOT (default) -6.283185307179586 1.516291874781302 0.5
GNUPLOT (default) -6.156252270670907 1.5490769687987143 0.5
GNUPLOT (default) -6.029319234162229 0.30753349072971314 0.5
GNUPLOT (default) -6.283185307179586 1.9916843919829947 0.5
GNUPLOT (default) -6.156252270670907 0.33627277530403243 0.5
GNUPLOT (default) -6.029319234162229 0.2532754844189571 0.5
GNUPLOT (default) -5.90238619765355 1.083699870620209 0.5
GNUPLOT (default) ...
GNUPLOT (default) EOD
GNUPLOT (default) reset
GNUPLOT (default) set key horizontal
GNUPLOT (default) set grid
GNUPLOT (default) set title 'My title'
GNUPLOT (default) set title "My title"
GNUPLOT (default) set xrange [-7:7]
GNUPLOT (default) set ylabel 'Y label'
GNUPLOT (default) set xlabel 'X label'
GNUPLOT (default) set ylabel "Y label"
GNUPLOT (default) set xlabel "X label"
GNUPLOT (default) plot \
$data0 w l t 'Real model' dt 2 lw 2 lc rgb 'red', \
$data1 w errorbars t 'Data'
GNUPLOT (default)
```
The **Gnuplot.jl** package (note the leading `GNUPLOT`...) tells us which commands are being sent to the gnuplot process and the name of the current gnuplot session (`default`). The **Gnuplot.jl** package will also print the replies from gnuplot, e.g.:
``` Julia

View File

@ -3,7 +3,7 @@ module Gnuplot
using StructC14N, ColorTypes, Printf, StatsBase, ReusePatterns, DataFrames
import Base.reset
import Base.write
import Base.println
import Base.iterate
import Base.convert
@ -12,7 +12,6 @@ export @gp, @gsp, save, contourlines, hist
# ╭───────────────────────────────────────────────────────────────────╮
# │ TYPE DEFINITIONS │
# ╰───────────────────────────────────────────────────────────────────╯
# ---------------------------------------------------------------------
mutable struct DataSet
name::String
@ -376,23 +375,23 @@ end
# ╭───────────────────────────────────────────────────────────────────╮
# │ write() and writeread() │
# │ println() and writeread() │
# ╰───────────────────────────────────────────────────────────────────╯
# ---------------------------------------------------------------------
"""
# write
# println
Send a string to gnuplot's STDIN.
The commands sent through `write` are not stored in the current
The commands sent through `println` are not stored in the current
session (use `newcmd` to save commands in the current session).
## Arguments:
- `gp`: a `DrySession` object;
- `str::String`: command to be sent;
"""
write(gp::DrySession, str::AbstractString) = nothing
function write(gp::GPSession, str::AbstractString)
println(gp::DrySession, str::AbstractString) = nothing
function println(gp::GPSession, str::AbstractString)
global state
if state.verbose
printstyled(color=:light_yellow, "GNUPLOT ($(gp.sid)) $str\n")
@ -404,8 +403,8 @@ function write(gp::GPSession, str::AbstractString)
end
write(gp::DrySession, d::DataSet) = nothing
function write(gp::GPSession, d::DataSet)
println(gp::DrySession, d::DataSet) = nothing
function println(gp::GPSession, d::DataSet)
if state.verbose
v = ""
printstyled(color=:light_black, "GNUPLOT ($(gp.sid)) $(d.name) << EOD\n")
@ -418,12 +417,9 @@ function write(gp::GPSession, d::DataSet)
end
printstyled(color=:light_black, "GNUPLOT ($(gp.sid)) EOD\n")
end
write(gp.pin, "\n")
write(gp.pin, "$(d.name) << EOD\n")
write(gp.pin, join(d.lines, "\n") * "\n")
write(gp.pin, "EOD\n")
write(gp.pin, "\n")
write(gp.pin, "\n")
flush(gp.pin)
return nothing
end
@ -434,17 +430,23 @@ writeread(gp::DrySession, str::AbstractString) = [""]
function writeread(gp::GPSession, str::AbstractString)
global state
verbose = state.verbose
state.verbose = false
write(gp, "print 'GNUPLOT_CAPTURE_BEGIN'")
write(gp, str)
write(gp, "print 'GNUPLOT_CAPTURE_END'")
println(gp, "print 'GNUPLOT_CAPTURE_BEGIN'")
state.verbose = verbose
println(gp, str)
state.verbose = false
println(gp, "print 'GNUPLOT_CAPTURE_END'")
state.verbose = verbose
out = Vector{String}()
while true
l = take!(gp.channel)
l == "GNUPLOT_CAPTURE_END" && break
push!(out, l)
end
state.verbose = verbose
return out
end
@ -457,7 +459,7 @@ function reset(gp::DrySession)
gp.datas = Vector{DataSet}()
gp.plots = [SinglePlot()]
gp.curmid = 1
write(gp, "reset session")
println(gp, "reset session")
return nothing
end
@ -478,7 +480,7 @@ function newdataset(gp::DrySession, accum::Vector{String}; name="")
name = "\$$name"
d = DataSet(name, accum)
push!(gp.datas, d)
write(gp, d) # Send now to gnuplot process
println(gp, d) # Send now to gnuplot process
return name
end
newdataset(gp::DrySession, args...; name="") = newdataset(gp, data2string(args...), name=name)
@ -488,7 +490,7 @@ newdataset(gp::DrySession, args...; name="") = newdataset(gp, data2string(args..
function newcmd(gp::DrySession, v::String; mid::Int=0)
setmulti(gp, mid)
(v != "") && (push!(gp.plots[gp.curmid].cmds, v))
(length(gp.plots) == 1) && (write(gp, v))
(length(gp.plots) == 1) && (println(gp, v))
return nothing
end
@ -531,39 +533,39 @@ end
# ---------------------------------------------------------------------
dump(gp::DrySession; kw...) = dump(gp, gp; kw...)
function dump(gp::DrySession, stream; term::AbstractString="", output::AbstractString="")
write(stream, "reset\n")
println(stream, "reset")
if term != ""
former_term = writeread(gp, "print GPVAL_TERM")[1]
former_opts = writeread(gp, "print GPVAL_TERMOPTIONS")[1]
write(stream, "set term $term\n")
println(stream, "set term $term")
end
(output != "") && write(stream, "set output '$output'\n")
(output != "") && println(stream, "set output '$output'")
if !(typeof(stream) <: DrySession)
# Dump datasets
for i in 1:length(gp.datas)
d = gp.datas[i]
write(stream, d.name * " << EOD\n")
println(stream, d.name * " << EOD")
for j in 1:length(d.lines)
write(stream, d.lines[j] * "\n")
println(stream, d.lines[j])
end
write(stream, "EOD\n")
println(stream, "EOD")
end
end
for i in 1:length(gp.plots)
d = gp.plots[i]
for j in 1:length(d.cmds)
write(stream, d.cmds[j] * "\n")
println(stream, d.cmds[j])
end
s = (d.flag3d ? "splot " : "plot ") * " \\\n " *
join(d.elems, ", \\\n ")
write(stream, s * "\n")
println(stream, s)
end
(length(gp.plots) > 1) && write(stream, "unset multiplot\n")
(output != "") && write(stream, "set output\n")
(length(gp.plots) > 1) && println(stream, "unset multiplot")
(output != "") && println(stream, "set output")
if term != ""
write(stream, "set term $former_term $former_opts\n")
println(stream, "set term $former_term $former_opts")
end
return output
end
@ -966,7 +968,6 @@ function setverbose(b::Bool)
end
# --------------------------------------------------------------------
"""
`save(...)`

View File

@ -73,10 +73,8 @@ s = Gnuplot.data2string(u, v, z)
#-----------------------------------------------------------------
x = collect(1.:100);
#-----------------------------------------------------------------
for i in 1:10
@gp :gp1 "plot sin($i*x)"
@gp :gp2 "plot sin($i*x)"