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/f4ccca01b0294bf577f5f4708966a05ac6999a97c1881e0e9d9143d98cbb1ec4.png

Line plots connect the data points

plot(xs, ys)
_images/f4ccca01b0294bf577f5f4708966a05ac6999a97c1881e0e9d9143d98cbb1ec4.png

Scatter plots show the data points only

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

you can trace functions directly

plot(f, xs)
_images/f4ccca01b0294bf577f5f4708966a05ac6999a97c1881e0e9d9143d98cbb1ec4.png

Trace a function within a range

plot(f, 0.0, 4pi)
_images/133ab8f2ba9237ab8a116041f00327e397784af4813df9da6d0ea35e9ed6e834.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/3e5cf81fa827b0e8a317e2c1da85b18edd0434f8a19793b584d6012cbf1a0183.png

Plotting two functions with customizations

plot(xs, [f, f2], label=["f1" "f2"], linecolor=[:black :green], title="Two time series")
_images/82b5d6d64903030599616330689ebd8e410517dd9dec2aedbe0eb9e99ae526ed.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/c0e2abd7663b2ae859c9b9a4cb576cc5a9d7473c9a00d59da45a18e4eb4ea65c.png

Parametric plot

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

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

Subplots

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

Subplot layout

fig = plot(ax1, ax2, layout=(2, 1))
_images/5eb3d056314512f1918f0bd6741a725e8066e4e4f881dd3de42d21f338ee58a5.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/0e82554b6de3486a9d692af105b0b5a432b041f572877ea6b8269e1f1bea11af.png

Save figure#

savefig([fig_obj,] filename)

Save the current figure

savefig("vfield.png")
"/home/github/actions-runner-1/_work/mmsb-bebi-5009/mmsb-bebi-5009/.cache/docs/vfield.png"

Save the figure fig

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

This notebook was generated using Literate.jl.