Gaston can create 2-D plots, including regular function plots, plots with logarithmic axes, scatter, stem and step plots, bar plots and histograms, and images.
2-D plots
plot
and plot!
commands
To plot a vector y
against a vector x
, use the command plot(x,y)
.
t = 0:0.01:1
plot(t, sin.(2π*5*t))
To add a second curve, use plot!
:
plot!(t,cos.(2π*5*t))
Curves are added to a figure one by one; the first curve is plotted with plot
, and the rest with succesive plot!
commands.
Plot configuration
Many plot properties can be configured. These properties are divided in axes properties (grid, axes labels, etc) and curve properties (color, line style, point type, etc). See the full list in Settings and Configuration.
Note that many of the configuration options follow gnuplot's syntax, and are specified as strings (even if they are numerical).
The axes configuration can be provided as arguments to plot
:
plot(t, sin.(2π*10*t),
title="Sine wave", grid="on", xlabel="Time(s)", ylabel="Amplitude (V)")
Curve properties can be mixed with axes properties:
plot(t, sin.(2π*3*t),
linewidth="2", linecolor="red", pointtype="ecircle", plotstyle="linespoints",
linestyle="-.-", xlabel="Time(s)", ylabel="Amplitude (V)")
In plots with multiple curves, the axes must be configured with the first plot
command; plot!
only allows configuration of the respective curve.
Legends
Legends can be added with keyoptions
and legend
:
t = -5:0.01:5
plot(t, sinc.(t), keyoptions="box top left", legend="Sinc(t)")
Setting the background
The background can also be configured:
t = -5:0.01:5
plot(t, sinc.(t), linecolor="yellow", linewidth="3", background="blue")
Ranges
Ranges can be specified with xrange
and yrange
:
t = -5:0.01:5
plot(t, sinc.(t), xrange="[-5:*]", yrange="[-1:1.5]")
Plot the $y=0$ and $x=0$ axes
A neat gnuplot feature exposed by Gaston is the ability to plot the $y=0$ and $x=0$ axes:
t = -5:0.01:5
plot(t, sinc.(t.+1), xzeroaxis="on", yzeroaxis="on")
Font and font size
The font and font size can be set with font
:
plot(t, sinc.(t), font="Consolas, 12")
Plot size
The plot size can be controlled with size
. (Be careful to use the correct units for your chosen terminal).
plot(t, sinc.(t), size="200,200")
Advanced configuration with gpcom
Although Gaston does not expose all of gnuplot's capabilities, arbitrary gnuplot commands can be specified using the gpcom
argument to plot
. This string is passed to gnuplot right before the plot
command is issued. An example is a custom tics specification:
t = -5:0.01:5
plot(t,sinc.(t),gpcom="set xtics -5,2,5; set ytics 0,0.5,1")
Setting default values
Default values can be configured with the set
command. For example,
set(linewidth="5")
will cause all subsequent curves to be plotted using a line width of 5. However, properties set in plot
arguments override the default properties.
Logarithmic plots
The axes can be configured to have a logarithmic scale, using axis=semilogy
, semilogx
, or loglog
.
plot(t, sin.(2π*10*t) ,axis="semilogx",
xlabel="log(time)", ylabel="Amplitude", grid="on")
Scatter plots
A scatter plot can be generated with the scatter
command:
c = 2rand(1000).-1 .+ im*(2rand(1000).-1)
p = filter(x->abs(x)<1, c)
scatter(real(p), imag(p), pointtype="fsquare",pointsize="0.25",
gpcom="set object ellipse at 0,0 size 2,2",
title="Random points within the unit circle")
Besides the standard pointtypes, any UTF-8 character may be used:
scatter(randn(30), randn(30), pointtype="λ")
Behind the scenes, scatter
calls plot
with the points
plotstyle.
Stem plots
Stem plots make it obvious one is plotting a discrete-time signal. The stem
command replicates the behavior of stem
in Matlab, Octave, et al:
t = -2:0.06:2
stem(t, sin.(2π*t),pointsize="0.75")
By default, the line color is blue and the lines are made sligthly thicker. If only the vertical lines ("impulses") are desired, pass the option onlyimpulses=true
to stem
:
stem(t, sin.(2π*t), onlyimpulses=true)
Behind the scenes, stem
calls plot
with the impulses
plotstyle, followed (if onlyimpulses == true
) by a call to plot!
with the points
plotstyle and the pointtype set to ecircle
.
Step plots
In step plots, data points are joined with a horizontal line. There are three variants: steps
, fsteps
, and fillsteps
.
t = -2:0.06:2
plot(t, sin.(2π*t), plotstyle="steps", title="Steps plot")
plot(t, sin.(2π*t), plotstyle="fillsteps",
fillstyle="solid 0.5", title="Fillsteps plot")
The color can be specified with fillcolor
:
plot(t, sin.(2π*t), plotstyle="fillsteps",
fillstyle="solid 0.75", fillcolor="plum", title="Fillsteps plot")
Bar plots
Bar plots can be generated with the bar
command:
year = range(1985, length=20);
data = 0.5 .- rand(20)
bar(year, data, gpcom="set xtics rotate")
Behind the scenes, bar
uses gnuplot's boxes
plotstyle. The bars' width, color and fillstyle can be controlled:
bar(year, data, gpcom="set xtics rotate", legend="Random number",
keyoptions = "box under", boxwidth="0.66",
fillstyle="pattern 2", fillcolor="dark-goldenrod")
Histograms
To plot histograms, use the histogram
command. This command takes the same properties as bar
. In addition, histogram
accepts a bins
parameter, used to specify the number of bins, and a norm
parameter that can be used to normalize the area under the histogram.
histogram(rand(10000), bins=15, norm=1, title="Histogram", yrange="[0:1.8]")
It is of course possible to use histogram
(or any other plot command) along with plot!
to produce different kinds of plots in the same figure:
x = -5:0.05:5
data = randn(10000)
gaussian = @. exp(-x^2/2)/sqrt(2π)
set(keyoptions="box top left")
histogram(data,bins=25,norm=1,
legend="Experimental",linecolor="turquoise",boxwidth="0.8 relative",
title="Experimental and Theoretical Gaussian distributions")
plot!(x,gaussian,linecolor="black",legend="Theoretical")
Images
The command to plot an image is imagesc
. It can plot a scaled or RGB image, depending on whether the provided coordinates are an array with two or with three dimensions. This command takes the properties title
, xlabel
, ylabel
, xrange
and yrange
. In addition, RGB images can take a parameter clim
, which must be a two-element array, and which is used to scale the image values.
Scaled image
A scaled image is a plot of a matrix whose elements are interpreted as colors.
Z = [5 4 3 1 0; 2 2 0 0 1; 0 0 0 1 0; 0 1 2 4 3]
imagesc(Z, title="Simple scaled image")
RGB image
An RGB image is a plot of a 3-D matrix whose elements are interpreted as the red, green, and blue components of each image pixel.
R = [x+y for x=0:5:120, y=0:5:120]
G = [x+y for x=0:5:120, y=120:-5:0]
B = [x+y for x=120:-5:0, y=0:5:120]
Z = zeros(25,25,3)
Z[:,:,1] = R
Z[:,:,2] = G
Z[:,:,3] = B
imagesc(Z, title="RGB Image", clim=[10,200], xrange="[1:25]", yrange="[1:25]")
Plotting with financial and error bars
Gaston supports plotting using financial and error bars.
y = [i+rand() for i=1:0.3:8]
open=y.-0.1*rand(length(y));
close=open.+1;
low=open.-1;
high=open.+1.5;
fin = Gaston.FinancialCoords(open,low,high,close)
plot(y, financial=fin,
title="Example of financial bars",plotstyle="financebars")
x = 0:2:50
y = @. 2.5x/(5.67+x)^2
err = Gaston.ErrorCoords(0.05*rand(length(x)))
plot(x,y,err=err,title="Example of error lines",plotstyle="errorlines")
Saving plots
To save a plot (or "print" it, in gnuplot's parlance), use the printfigure
command:
printfigure(term="png", font="Consolas,10", size="1280,900",
linewidth="1", background="gray", outputfile="myfigure.png")
The outputfile
argument is required; by default the figure is saved in pdf format.