Julia Plotting Guide — Plots.jl, GR, and Data Visualization
In this tutorial, you will learn about Julia Plotting Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Julia Plots.jl is a meta-package providing a consistent API across backends (GR for speed, PyPlot for MATLAB-like, Plotly for interactivity) -- supporting plot, scatter, histogram, heatmap, and custom layouts through attributes and series types.
Basic Plotting
using Plots
# Line plot
plot(sin, 0:0.1:2π, label="sin(x)")
plot!(cos, 0:0.1:2π, label="cos(x)")
# From data
x = 1:10
y = rand(10)
plot(x, y, marker=:circle, linestyle=:dash)
Plot Types
# Scatter
scatter(x, y, color=:red, markersize=5)
# Histogram
histogram(randn(1000), bins=30, normalize=true)
# Bar chart
bar(["A", "B", "C"], [3, 7, 2])
# Heatmap
heatmap(rand(10, 10), color=:viridis)
# Contour
contour(-3:0.1:3, -3:0.1:3, (x,y) -> x*exp(-(x^2+y^2)))
Customization
plot(
sin, 0:0.1:2π,
title = "Sine Wave",
xlabel = "x",
ylabel = "sin(x)",
label = "sin",
linewidth = 2,
linecolor = :blue,
linestyle = :solid,
legend = :bottomright,
grid = true,
size = (800, 400)
)
Subplots
# Layout
p1 = plot(sin, title="Sin")
p2 = plot(cos, title="Cos")
p3 = plot(tan, title="Tan")
plot(p1, p2, p3, layout=(3, 1), size=(800, 600))
# Grid layout
l = @layout [a b; c]
plot(p1, p2, p3, layout=l)
Save and Export
# Save plot
p = plot(sin)
savefig(p, "plot.png")
savefig(p, "plot.svg")
savefig(p, "plot.pdf")
# High DPI
savefig(p, "plot.png", dpi=300)
Common Mistakes
1. Forgetting to install backend
Plots.jl needs a backend: ] add Plots GR or using PyPlot. Without a backend, plot commands error.
2. Overwriting plots
plot!() (with !) adds to the current plot. plot() creates a new plot. Using plot() twice in a row overwrites.
3. Large datasets
Plotting 1M points is slow. Thin data before plotting or use plot(x, y, markersize=0.5) for dense data.
Practice Questions
1. How do you create a line plot?
plot(x, y) or plot(func, range). Use plot! to add to an existing plot.
2. How do you create a histogram?
histogram(data, bins=n) creates a histogram with n bins.
3. How do you save a plot to a file?
savefig(plot_obj, "filename.png") or just savefig("filename.png") for the current plot.
FAQ
{{< faq question="Which backend should I use?" >}} GR for speed (default), PyPlot for MATLAB compatibility, Plotly for interactive web plots. {{< /faq >}}
{{< faq question="How do I change colors?" >}}
Use the color attribute: plot(x, y, color=:red). Available as :red, :blue, :green, or hex "#FF0000".
{{< /faq >}}
{{< faq question="Can I create 3D plots?" >}}
Yes. Use plot(x, y, z, seriestype=:surface) for surfaces or plot(x, y, z, seriestype=:scatter) for 3D scatter.
{{< /faq >}}
What's Next
Now learn about DataFrames in Julia.
| Topic | Description | Link |
|---|---|---|
| DataFrames | Tabular data handling | {{< ref "14-dataframes" >}} |
| Concurrency | Parallel computing | {{< ref "15-concurrency" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro