Plotting with Julia

Plotting with Julia#

using Plots

Prepare data then plot

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

plot(xs, ys)
_images/444b7d6e30a8d1122fd729fd4bddd73f268e17ca93d95ef4386da3d7c19d6258.png

Line plots connect the data points

plot(xs, ys)
_images/444b7d6e30a8d1122fd729fd4bddd73f268e17ca93d95ef4386da3d7c19d6258.png

Scatter plots show the data points only

scatter(xs, ys)
_images/25b3f25be71bcabcbd07835549c94e138c337fb85663b318595f312b2028dcdd.png

you can trace functions directly

plot(f, xs)
_images/444b7d6e30a8d1122fd729fd4bddd73f268e17ca93d95ef4386da3d7c19d6258.png

Trace a function within a range

plot(f, 0.0, 4pi)
_images/fabd76b83a4b0011ca1d5f5fea377dfe2e3fe9283f0c748429f9b72babff8a29.png

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)
_images/765c06a674f1e092c23dac545b47a187c0ed995bbe8ff484e697e52755b2ed68.png

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])
_images/3141eefa7b7ccaf9d69f9b870d700a5c269e4c7de6f0f7c08cb295639ba60fd8.png

Plotting two functions with customizations

plot(xs, [f, f2], label=["f1" "f2"], linecolor=[:black :green], title="Two time series")
_images/508e21b67a0eb05853bbdc05c74901657df01d60b75993ba04aac8865f4ce440.png

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)
_images/10d7a34733ae0db8478a0870be8910f0d764a6ecb81c6f3a6af849d8ebbd23c6.png

Parametric plot

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

plot(xₜ, yₜ, 0, 2π, leg=false, fill=(0,:orange))
_images/1803d968181a9e6d5ed57ff9c7bd9db395d82a46033de3b526a3bf6d9753c346.png

Subplots

ax1 = plot(f, xs)
ax2 = plot(f2, xs)
plot(ax1, ax2)
_images/cbfe196c0cfebb61b83dbc3514853b6ace4665a2261beb2dd2f1b931466c7fb3.png

Subplot layout

fig = plot(ax1, ax2, layout=(2, 1))
_images/a220019be8d5045df2e6469fcb969dcf4668314b1082a4e4b3f3995b73649a80.png

Vector field#

Plots.jl#

# Quiver plot
quiver(vec(x2d), vec(y2d), quiver=(vec(vx2d), vec(vy2d))
# Or if you have a function f(x,y) -> (vx, vy)
quiver(x2d, y2d, quiver=f)

PythonPlot.jl:#

using PythonPlot as plt
plt.quiver(X2d, Y2d, U2d, V2d)

matplotlib: quiver plot

using Plots

∇ = \nabla

function ∇f(x, y; scale=(x^2 + y^2)^0.25 * 3)
    return [-y, x] ./ scale
end
∇f (generic function with 1 method)

x and y grid points

r = -1.0:0.2:1.0
xx = [x for y in r, x in r]
yy = [y for y in r, x in r]
11×11 Matrix{Float64}:
 -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0  -1.0
 -0.8  -0.8  -0.8  -0.8  -0.8  -0.8  -0.8  -0.8  -0.8  -0.8  -0.8
 -0.6  -0.6  -0.6  -0.6  -0.6  -0.6  -0.6  -0.6  -0.6  -0.6  -0.6
 -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4  -0.4
 -0.2  -0.2  -0.2  -0.2  -0.2  -0.2  -0.2  -0.2  -0.2  -0.2  -0.2
  0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
  0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2   0.2
  0.4   0.4   0.4   0.4   0.4   0.4   0.4   0.4   0.4   0.4   0.4
  0.6   0.6   0.6   0.6   0.6   0.6   0.6   0.6   0.6   0.6   0.6
  0.8   0.8   0.8   0.8   0.8   0.8   0.8   0.8   0.8   0.8   0.8
  1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0   1.0

Vector fields

quiver(xx, yy, quiver=∇f, aspect_ratio=:equal, line=(:black), arrow=(:closed))
_images/e5fb5731b5234922213c96f7412a4fe34806201664ee7c0205a34462ef373815.png

Save figure#

savefig([fig_obj,] filename)

Save the current figure

savefig("vfield.png")
"/home/runner/work/mmsb-bebi-5009/mmsb-bebi-5009/.cache/docs/vfield.png"

Save the figure fig

savefig(fig, "subplots.png")
"/home/runner/work/mmsb-bebi-5009/mmsb-bebi-5009/.cache/docs/subplots.png"

This notebook was generated using Literate.jl.