logo

Drawing Subplots with a Layout in Julia 📂Julia

Drawing Subplots with a Layout in Julia

Overview

In Julia, options related to subplots can be controlled through the layout option.

  • Entering an integer automatically creates a grid of that many plots.
  • Entering a 2-tuple of integers creates a grid exactly as specified.
  • The @layout macro is used to create complex layouts of the Plots.GridLayout type.

Code

using Plots

left = plot(randn(100), color = :red)
right = plot(randn(100), color = :blue)
plot(left, right)
png("easyone")

data = rand(10, 6)
plot(data, layout = 6)
png("easytwo")
plot(data, layout = (3,2))
png("easygrid")

l = @layout [p1 ; p2 p2]
p = plot(
        plot(rand(10)),
        plot(rand(100)),
        plot(rand(1000)),
        layout = l)
png("hardgrid")

Simple Enumeration

left = plot(randn(100), color = :red)
right = plot(randn(100), color = :blue)
plot(left, right)

easyone.png

Just by grouping several plots together and plotting again, it becomes a subplot.

Simple Layout layout

plot(data, layout = 6)

easytwo.png

plot(data, layout = (3,2))

easygrid.png

You can simply use an integer or provide a tuple to create the desired grid. Since it works the same way without providing an integer, it’s only necessary to remember the case where a tuple is given.

Complex Layout @layout

l = @layout [p1 ; p2 p2]

Defined a Plots.GridLayout type l that directs the layout such that there’s one picture in the first row and two columns of pictures in the second row. By providing this as an input to layout, a much more complex layout is represented like this.

p = plot(
        plot(rand(10)),
        plot(rand(100)),
        plot(rand(1000)),
        layout = l)

hardgrid.png

Creating Empty Spaces _

In the example above, if you want to center the picture in the first row to be the same size as those below, you can create empty spaces on both sides. You can indicate an empty space with _. If you adjust the width to half of the total with {0.5w},

l = @layout([_ p{0.5w} _; p p])

plot(
    plot(rand(10)),
    plot(rand(100)),
    plot(rand(1000)),
    layout = l)

Environment

  • OS: Windows
  • julia: v1.6.3