## 6.3 Create Plot Figure

The basic container object in Makie is Figure, a canvas where we can add objects like Axis, Colorbar, Legend, etc.

For Figure, we have have attributes like backgroundcolor, resolution, font and fontsize as well as the figure_padding which changes the amount of space around the figure content, see the colored area in Figure 6. It can take one number for all sides, or a tuple of four numbers for left, right, bottom and top. Let’s dive into these components.

### 6.3.1 Figure

function figure_canvas()
fig = Figure(;
backgroundcolor=:snow2,
resolution=(600,400),
)
end
JDS.figure_canvas()

### 6.3.2 Figure + Axis

Axis has a lot more, some of them are backgroundcolor, xgridcolor and title. For a full list just type help(Axis).

function figure_axis()
fig = Figure(;
backgroundcolor=:snow2,
resolution=(600,400),
)
ax = Axis(fig[1,1];
xlabel="x",
ylabel="y",
title="Title",
)
fig
end
JDS.figure_axis()

You can hide decorations and axis’ spines with:

• hidedecorations!(ax; kwargs...)
• hidexdecorations!(ax; kwargs...)
• hideydecorations!(ax; kwargs...)
• hidespines!(ax; kwargs...)

Remember, we can always ask for help to see what kind of arguments we can use, e.g.,

help(hidespines!)
  hidespines!(la::Axis, spines::Symbol... = (:l, :r, :b, :t)...)

Hide all specified axis spines. Hides all spines by default, otherwise
choose with the symbols :l, :r, :b and :t.

hidespines! has the following function signatures:

(Vector, Vector)
(Vector, Vector, Vector)
(Matrix)

Available attributes for Combined{Makie.hidespines!} are:



Alternatively, for decorations

help(hidedecorations!)
  hidedecorations!(la::Axis)

Hide decorations of both x and y-axis: label, ticklabels, ticks and grid.

hidedecorations! has the following function signatures:

(Vector, Vector)
(Vector, Vector, Vector)
(Matrix)

Available attributes for Combined{Makie.hidedecorations!} are:



For elements that you don’t want to hide, pass false, e.g., hideydecorations!(ax; ticks=false, grid=false).

Setting limits at once or independently for each axis is done by calling

• limits!(ax; l, r, b, t), where l is left, r right, b bottom, and t top.

You can also do ylims!(low, high) or xlims!(low, high), and even open ones by doing ylims!(low=0) or xlims!(high=1).

### 6.3.3 Figure + Axis + plot

Now, we add a plotting function into our new Axis:

function figure_axis_plot()
fig = Figure(;
backgroundcolor=:snow2,
resolution=(600,400),
)
ax = Axis(fig[1,1];
xlabel="x",
ylabel="y",
title="Title",
)
lines!(ax, 0.5:0.2:3pi, x -> cos(x)/x;
color=:black,
linewidth=2,
linestyle=:dash,
)
fig
end
JDS.figure_axis_plot()

This example already includes many of the attributes that are typically used. Additionally, it would be beneficial to include a “legend” for reference, especially if the example has more than one function. This will make it easier to understand.

### 6.3.4 Figure + Axis + plot + axislegend

So, let’s append another mutation plot object and add the corresponding legends by calling axislegend. This will collect all the labels you might have passed to your plotting functions and by default will be located in the right top position. For a different one, the position=:ct argument is called, where :ct means let’s put our label in the center and at the top, see Figure Figure 8:

function figure_axis_plot_leg()
fig = Figure(;
backgroundcolor=:snow2,
resolution=(600,400),
)
ax = Axis(fig[1,1];
xlabel="x",
ylabel="y",
title="Title",
xgridstyle=:dash,
ygridstyle=:dash,
)
lines!(ax, 0.5:0.2:3pi, x -> cos(x)/x;
linewidth=2,
linestyle=:solid,
label = "cos(x)/x",
)
scatterlines!(ax, 0.5:0.2:3pi, x -> -cos(x)/x;
color=:black,
linewidth=2,
linestyle=:dash,
label = "-cos(x)/x",
)
axislegend("legend"; position=:rt)
fig
end
JDS.figure_axis_plot_leg()

Other positions are also available by combining left(l), center(c), right(r) and bottom(b), center(c), top(t). For instance, for left top, use :lt.

However, having to write this much code just for two lines is cumbersome. So, if you plan on doing a lot of plots with the same general aesthetics, then setting a theme will be better. We can do this with set_theme!() as follows:

set_theme!(;
resolution=(600,400),
backgroundcolor=(:mistyrose, 0.1),
fontsize=16,
Axis=(;
xlabel="x",
ylabel="y",
title="Title",
xgridstyle=:dash,
ygridstyle=:dash,
),
Legend=(;
bgcolor=(:grey, 0.1),
framecolor=:orangered,
),
)
nothing

Plotting the previous figure should take the new default settings defined by set_theme!(kwargs):

function fig_theme()
fig = Figure()
ax = Axis(fig[1,1])
lines!(ax, 0.5:0.2:3pi, x -> cos(x)/x;
linewidth=2,
linestyle=:dash,
label = "cos(x)/x",
)
scatterlines!(ax, 0.5:0.2:3pi, x -> -cos(x)/x;
color=:black,
linewidth=2,
linestyle=:dash,
label = "-cos(x)/x",
)
axislegend("legend"; position=:rt)
fig
set_theme!()
end
JDS.fig_theme()

Note that the last line is set_theme!(), will reset the default’s settings of Makie. For more on themes please go to Section 6.5.

### 6.3.5 Figure + Axis + plot + Legend + Colorbar

Before moving on into the next section, it’s worthwhile to see an example where an array of attributes is passed at once to a plotting function. For this example, we will use the scatter plotting function to do a bubble plot.

The data for this could be an array with 100 rows and 3 columns, which we generated at random from a normal distribution. Here, the first column could be the positions in the x axis, the second one the positions in y and the third one an intrinsic associated value for each point. The latter could be represented in a plot by a different color or with a different marker size. In a bubble plot we can do both.

using Random: seed!
seed!(28)
xyz = randn(100, 3)
xyz[1:4, :]
4×3 Matrix{Float64}:
0.550992   1.27614    -0.659886
-1.06587   -0.0287242   0.175126
-0.721591  -1.84423     0.121052
0.801169   0.862781   -0.221599

Next, the corresponding plot can be seen in Figure 10:

function fig_bubbles(xyz)
fig = Figure(resolution=(600,400))
ax = Axis(fig[1,1]; aspect = DataAspect())
pltobj = scatter!(xyz[:, 1], xyz[:, 2];
color=xyz[:, 3],
label="Bubbles",
colormap=:plasma,
markersize=15 * abs.(xyz[:, 3]),
)
limits!(-3, 3, -3, 3)
Legend(fig[1, 2], ax, valign=:top)
Colorbar(fig[1, 2], pltobj, height=Relative(3/4))
fig
end
JDS.fig_bubbles(xyz)

where we have decomposed the tuple FigureAxisPlot into fig, ax, pltobj, in order to be able to add a Legend and Colorbar outside of the plotted object. We will discuss layout options in more detail in Section 6.8.

We have done some basic but still interesting examples to show how to use Makie.jl and by now you might be wondering: what else can we do?

CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso