Plotting with Plots.jl and Makie
Prerequisites: Data analysis with DataFrames
Julia has two major plotting ecosystems:
- Plots.jl — one simple API on top of several backends (GR by default, plus Plotly, PythonPlot, and others). Great for quick plots.
- Makie.jl — a fast, highly customizable system for publication-
quality figures, interactive 3D graphics, and animations. Choose a
backend package:
CairoMakie(static images),GLMakie(interactive windows, GPU), orWGLMakie(the browser).
A first plot with Plots.jl
# illustrative — needs the Plots package and opens a plot window
using Plots
x = range(0, 2π, length = 200)
plot(x, sin.(x), label = "sin", linewidth = 2)
plot!(x, cos.(x), label = "cos", linestyle = :dash)
xlabel!("x")
ylabel!("value")
title!("Trigonometric functions")
savefig("trig.png")
plotcreates a figure;plot!(with!) adds to the current one.savefigwrites PNG, SVG, or PDF depending on the extension.scatter,bar,histogram,heatmap, andsurfacemake other chart types.
The same with Makie
# illustrative — needs the CairoMakie package
using CairoMakie
x = range(0, 2π, length = 200)
fig = Figure(size = (700, 400))
ax = Axis(fig[1, 1], xlabel = "x", ylabel = "value", title = "Trigonometric functions")
lines!(ax, x, sin.(x), label = "sin")
lines!(ax, x, cos.(x), label = "cos", linestyle = :dash)
axislegend(ax)
save("trig.png", fig)
Makie separates the figure (the canvas and layout), axes, and
plots you add to axes, which makes multi-panel layouts easy:
fig[1, 2] is a second axis to the right.
Plotting a DataFrame
# illustrative — needs the DataFrames and StatsPlots packages
using DataFrames, StatsPlots
df = DataFrame(month = 1:6, sales = [120, 135, 128, 160, 172, 190])
@df df plot(:month, :sales, marker = :circle, label = "sales")
StatsPlots.jl adds the @df macro and statistical plots (box plots,
violin plots, density plots) to Plots.jl; AlgebraOfGraphics.jl is a
grammar-of-graphics layer for Makie, similar to ggplot2 in
R.
Time to first plot
The first plot in a new session takes a few seconds because the plotting code has to load and compile. Julia 1.9 and later cache much more compiled code, so this is far faster than it used to be — and later plots are instant. Keep one Julia session open (the VS Code REPL or a Pluto notebook) while you work.
What's next
Try the Intermediate Projects, then continue to Level 3 — Advanced: performance, metaprogramming, parallel computing, and Julia's scientific machine-learning ecosystem.