3-D Plots

3-D plots

Gaston can plot 3-D surfaces using the surf command; it accepts the same properties as plot, but it adds zlabel and semilogz axis properties, and a pm3d plotstyle. In addition, surf! allows plotting multiple surfaces in the same figure, and contour can display simple contour maps.

These commands require vector x and y coordinates. The z coordinate can be passed explicitly (as a matrix), or as a function.

Explicit x, y and z coordinates

The first way to obtain a 3-D plot is by giving explicit x, y and z coordinates to surf.

x=[0,1,2,3]
y=[0,1,2]
Z=[10 10 10; 10 5 10; 10 1 10; 10 0 10]
surf(x, y, Z, title = "3D: Valley of the Gnu from gnuplot manual")
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 0 0.5 1 1.5 2 2.5 3 0 0.5 1 1.5 2 0 2 4 6 8 10 3D: Valley of the Gnu from gnuplot manual gnuplot_plot_1

The default plostyle is to plot a mesh with points joined by lines, as seen above.

z coordinates defined by a function

Alternatively, a function may be provided that takes the x, y coordinates as arguments and returns a z coordinate.

x = y = -15:0.33:15
surf(x, y, (x,y)->sin.(sqrt.(x.*x+y.*y))./sqrt.(x.*x+y.*y),
    title="Sombrero", plotstyle="pm3d")
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 -15 -10 -5 0 5 10 -15 -10 -5 0 5 10 -0.2 0 0.2 0.4 0.6 0.8 Sombrero gnuplot_plot_1 -0.2 0 0.2 0.4 0.6 0.8

Plotting multiple surfaces with surf!

The equivalent to plot! is surf!:

x = y = -10:0.5:10
surf(x, y, (x,y)->2sin.(sqrt.(x.*x+y.*y))./sqrt(x.*x+y.*y)-2,
    title="Two 3D plots in a single figure",
    plotstyle="lines", linecolor="magenta", gpcom="unset colorbox")
surf!(x,y,(x,y)->cos.(x/2).*sin.(y/2)+3,plotstyle="pm3d")
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 -10 -5 0 5 10 -10 -5 0 5 10 -2 -1 0 1 2 3 4 Two 3D plots in a single figure gnuplot_plot_1 gnuplot_plot_2

Changing the palette

The palette of a 3-D plot with pm3d plotstyle can be controlled with the palette setting.

surf(x, y, (x,y)->sin.(sqrt(x.*x+y.*y))./sqrt.(x.*x+y.*y),
     title="Sombrero", plotstyle="pm3d", palette="gray")
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 -10 -5 0 5 10 -10 -5 0 5 10 -0.2 0 0.2 0.4 0.6 0.8 Sombrero gnuplot_plot_1 -0.2 0 0.2 0.4 0.6 0.8

Plotting contours

Gnuplot's contour support is quite flexible. Currently, Gaston exposes just basic contour functionality with little room for configuration.

A contour for the current surface can be plot using gpcom="set contour base":

gp = "set contour base; unset key"
surf(x, y, (x,y)->5cos.(x/2).*sin.(y/2), font=",10", gpcom=gp)
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 -10 -5 0 5 10 -10 -5 0 5 10 -5 -4 -3 -2 -1 0 1 2 3 4 5 gnuplot_plot_1

The contour command plots just the contour lines, not the surface:

x = y = -5:0.1:5;
contour(x, y, (x,y)->5cos.(x/2).*sin.(y/2))
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 gnuplot_plot_1 gnuplot_plot_2 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -3 -4 -4 -4 -4 -4 -4 -2 0 2 4 -4 -2 0 2 4

The labels can be disabled if labels=false is passed as an argument:

contour(x, y, (x,y)->5cos.(x/2).*sin.(y/2), labels=false)
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 gnuplot_plot_1 -4 -2 0 2 4 -4 -2 0 2 4

Plotting heatmaps

Gaston does not expose a direct interface for plotting heatmaps, but these are easily achieved by setting gnuplot's view to map using gpcom:

surf(x, y, (x,y)->cos.(x/2).*sin.(y/2), plotstyle="pm3d", gpcom="set view map")
Gnuplot Produced by GNUPLOT 5.2 patchlevel 7 gnuplot_plot_1 -4 -2 0 2 4 -4 -2 0 2 4 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1