Line & Scatter plots

Line and Scatter plots

Syntax

The relevant commands here are

The general syntax is:

plot(data_to_plot...; options...)

A command with an exclamation mark will add the corresponding plot to the current active axes while a command without will erase any existing plot on the current active axes and then display the plot.

For instance:

x = range(-2.5, stop=2.5, length=100)
y = @. exp(-x^2) * sin(x)
plot(x, y)
mask = 1:5:100
scatter!(x[mask], y[mask])

overlays a scatterplot to a line plot:

Data formats

The table below summarises the different ways you can specify what data to plot, they are discussed in more details and with examples further on.

FormExampleComment
single vector $x$plot(randn(5))pairs $(i, x_i)$
two vectors $x,y$plot(randn(5),randn(5))pairs $(x_i,y_i)$
multiple vectors $x,y,z$plot(randn(5),randn(5),randn(5))pairs $(x_i,y_i)$, $(x_i,z_i)$, ...
single matrix $X$plot(randn(5,2))pairs $(i, x_{i1})$, $(i, x_{i2})$, ...
one vector then vectors or matricesplot(1:5, randn(5,2), randn(5))pairs between the first vector and subsequent columns
function $f$ from toplot(sin, 0, pi)draws points $x_i$ on the interval and plots pairs $(x_i, f(x_i))$

Examples

For instance:

plot(randn(5))

For instance:

x = range(0, stop=1, length=100)
plot(x, x.^2, x.^3, x.^4)

For instance:

plot(randn(10, 3))

For instance:

x = range(0, stop=1, length=25)
y = @. sin(x)
z = @. cos(x)
t = y .+ z
scatter(x, hcat(y, z), t)

For instance:

scatter(sin, 0, 2π; msize=0.1)
xlim(0,2π)

Styling options

Line and scatter plots have effectively two things they can get styled:

  1. the line styles
  2. the marker styles

Note the plural, so that if you are plotting multiple lines at once, each keyword accepts a vector of elements to style the individual plots. If a styling option is specified with a single value but multiple lines are being plotted, all will have that same value for the relevant option.

For instance:

plot(randn(10, 3), colors=["violet", "navyblue", "orange"], lwidth=0.1)

Note

GPlot typically accepts multiple aliases for option names, pick whichever one you like, that sticks best to mind or that you find the most readable.

Line style options

For each of these options, it should be understood that you can either pass a single value or a vector of values (see the note at the beginning of the section).

ValueResult
"-"
"--"
"-."
"none"
ValueResult
0.001
0.01
0.05
0.1
0
ValueResult
"cornflowerblue"
"forestgreen"
"indigo"
"RGB(0.5,0.7,0.2)"

Note that if the colour is not specified, a default colour will be taken by cycling through a colour palette.

x = range(-2, stop=2, length=20)
y1 = @. sin(exp(-x)) + 0.5
y2 = @. sin(exp(-x)) - 0.5
plot(x, y1; label="unsmoothed")
plot!(x, y2; smooth=true, label="smoothed")
legend()

Here's another example combining several options:

x = range(0, stop=2, length=25)
for α ∈ 0.01:0.05:0.8
    plot!(x, x.^α, lwidth=α/10, col=RGB(0.0,0.0,α), smooth=true)
end

Marker style options

ValueResult
"o" or "circle"
"." or "fo" or "fcircle"
"^" or "triangle"
"f^" or "ftriangle"
"s" or "square"
"fs" or "fsquare"
"x" or "cross"
"+" or "plus"
ValueResult
0.1
0.25
0.5

Notes

Missing, Inf or NaN values

If the data being plotted contains missing or Inf or NaN, these values will all be treated the same way: they will not be displayed.

y = [1, 2, 3, missing, 3, 2, 1, NaN, 0, 1]
plot(y, marker="o")
ylim(-1, 4)

Modifying the underlying data

Plotting objects are tied to the data meaning that if you do an in place modification of a vector that is currently being plotted, and you refresh the plot, the plot will change accordingly.

y = [1, 2, 3, 4, 5, 6]
plot(y, mcol="red")
y[3] = 0

This only happens for in-place modification; note the difference with the example below:

y = [1, 2, 3, 4, 5, 6]
plot(y, mcol="red")
y = 0