Contents

Julia | Plotting

Notes about Julia plotting for future self. And I’m not duplicating other’s work except for the structure of this post.

Plotting libraries in Julia

The most famous ones are:

Other libraries I just saw and have not researched fully:

Reference

Tips about Plots.jl

You don’t have to generate data points

Plots.jl suppots function plotting.

  • plot(f, tmin, tmax) or plot(f, tArray)
  • plot(fx, fy, tmin, tmax) or plot(fx, fy, tArray)

For example, you can easily draw a parametric plot like this

1
2
# plotting (x(t), y(t))
plot(sin, (x-> sin(2x)), 0, 2π, line = 4, leg = false)

Or make a contour plot without precalculating the function values

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
x = 1:0.5:20
y = 1:0.5:10

g(x, y) = (3x + y ^ 2) * abs(sin(x) + cos(y))

# Precalculate the value
X = repeat(reshape(x, 1, :), length(y), 1)
Y = repeat(y, 1, length(x))
Z = map(g, X, Y)
p2 = contour(x, y, Z)

# Generate z value on-the-fly
p1 = contour(x, y, g, fill=true)
plot(p1, p2)

Subplots

You can combine multiple plots into one single plot.

1
2
3
4
5
l = @layout [a ; b c]
p1 = plot(...)
p2 = plot(...)
p3 = plot(...)
plot(p1, p2, p3, layout = l)

See also layouts for more options.

Supported styles

From this tutorial.

1
2
3
# tip: use Plots.supported_styles() or Plots.supported_markers() to see which linestyles or markershapes you can use
@show Plots.supported_styles();
@show Plots.supported_markers();

Images

1
2
3
4
# usingPkg; Pkg.add("Images")
using Images
img1 = load("dog.jpg")
plot(img1)

Tips about PyPlot.jl

Dealing with installation errors

Since PyPlot.jl depends on the Python package matplotlib, sometimes simply ]add the package will not work due to some quirks in the installation process.

It is recommended to have a clean Conda environment inside Julia to minimize installation issues.

To enforce a conda environment inside Julia, set the PYTHON environment variable to an empty string.12

1
ENV["PYTHON"]=""

Finally rebuild the related packages.

1
2
import Pkg
Pkg.build(["PyCall", "Conda", "PyPlot"])

  1. https://github.com/JuliaPy/PyCall.jl ↩︎

  2. You can also add this line to ~/.julia/config/startup.jl to ensure Julia would use its own Python environment forever. ↩︎