Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

using Plots

Prepare data then plot

f(x) = sin(sin(x) + 1)
xs = 0.0:0.1:4pi
ys = f.(xs)

plot(xs, ys)
Plot{Plots.GRBackend() n=1}

Line plots connect the data points

plot(xs, ys)
Plot{Plots.GRBackend() n=1}

Scatter plots show the data points only

scatter(xs, ys)
Plot{Plots.GRBackend() n=1}

you can trace functions directly

plot(f, xs)
Plot{Plots.GRBackend() n=1}

Trace a function within a range

plot(f, 0.0, 4pi)
Plot{Plots.GRBackend() n=1}

Customization example

plot(f, xs,
    label="My line", legend=:bottom,
    title="My Title",  line=(:red, 3),
    xlim = (0.0, 5.0), ylim = (-1.0, 1.5),
    xlabel="Time", ylabel="My Mood", border=:box)
Plot{Plots.GRBackend() n=1}

Multiple series: each row is one observation; each column is a variable.

f2(x) = cos(cos(x) + 1)
y2 = f2.(xs)
plot(xs, [ys y2])
Plot{Plots.GRBackend() n=2}

Plotting two functions with customizations

plot(xs, [f, f2], label=["f1" "f2"], linecolor=[:black :green], title="Two time series")
Plot{Plots.GRBackend() n=2}

Building the plot in multiple steps in the object-oriented way

xMin = 0.0
xMax = 4.0π
fig = plot(f, xMin, xMax, label="f1", lc=:black)
plot!(fig , f2, xMin, xMax, label="f2", lc=:lightsalmon)
plot!(fig, title = "My title", legend=:outertop)
Plot{Plots.GRBackend() n=2}

Parametric plot

xₜ(t) = sin(t)
yₜ(t) = sin(2t)

plot(xₜ, yₜ, 0, 2π, leg=false, fill=(0,:orange))
Plot{Plots.GRBackend() n=1}

Subplots

ax1 = plot(f, xs)
ax2 = plot(f2, xs)
plot(ax1, ax2)
Plot{Plots.GRBackend() n=2}

Subplot layout

fig = plot(ax1, ax2, layout=(2, 1))
Plot{Plots.GRBackend() n=2}

Vector field

Plots.jl

# Quiver plot
quiver(vec(x2d), vec(y2d), quiver=(vec(vx2d), vec(vy2d))

# Or if you have a gradient function ∇f(x,y) -> (vx, vy)
quiver(x2d, y2d, quiver=∇f)
using Plots
using UniformStreamlines

∇ = \nabla <TAB>

∇fx = (x, y) -> -y
∇fy = (x, y) -> 1 + x - y^2
#5 (generic function with 1 method)

x and y grid points

xs = LinRange(-2, 2, 200)
ys = LinRange(-2, 2, 200)
200-element LinRange{Float64, Int64}: -2.0, -1.9799, -1.9598, -1.9397, …, 1.9196, 1.9397, 1.9598, 1.9799, 2.0

stream object

str = evenstream(xs, ys, ∇fx, ∇fy)
StreamlineData{2}: 99 streamlines, 35634 points, domain [-2.0, 2.0] × [-2.0, 2.0]

Stream plot

streamlines(str; line_z=colorize(str, :norm), color=:viridis, with_arrows=true, colorbar=false)
Plot{Plots.GRBackend() n=2}

Save figure

savefig([fig_obj,] filename)

Save the current figure

savefig("vector-field.png")
"/home/github/actions-runner-2/_work/mmsb-bebi-5009/mmsb-bebi-5009/docs/vector-field.png"

Save the figure fig

savefig(fig, "subplots.png")
"/home/github/actions-runner-2/_work/mmsb-bebi-5009/mmsb-bebi-5009/docs/subplots.png"

This notebook was generated using Literate.jl.