## 5.2 Attributes

A custom plot can be created by using attributes. The attributes can be set through keyword arguments. A list of attributes for every plotting object can be viewed via:

fig, ax, pltobj = scatterlines(1:10)
pltobj.attributes
Attributes with 15 entries:
color => RGBA{Float32}(0.0,0.447059,0.698039,1.0)
colormap => viridis
colorrange => Automatic()
cycle => [:color]
inspectable => true
linestyle => nothing
linewidth => 1.5
marker => Circle
markercolor => Automatic()
markercolormap => viridis
markercolorrange => Automatic()
markersize => 9
model => Float32[1.0 0.0 0.0 0.0; 0.0 1.0 0.0 0.0; 0.0 0.0 1.0 0.0; 0.0 0.0 0.0 1.0]
strokecolor => black
strokewidth => 0

Or as a Dict calling pltobj.attributes.attributes.

Asking for help in the REPL as ?lines or help(lines) for any given plotting function will show you their corresponding attributes plus a short description on how to use that specific function. For example, for lines:

help(lines)
  lines(positions)
lines(x, y)
lines(x, y, z)

Creates a connected line plot for each element in (x, y, z), (x, y) or
positions.

│ Tip
│
│  You can separate segments by inserting NaNs.

lines has the following function signatures:

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

Available attributes for Lines are:

color
colormap
colorrange
cycle
depth_shift
diffuse
inspectable
linestyle
linewidth
nan_color
overdraw
shininess
space
specular
ssao
transparency
visible

Not only the plot objects have attributes, also the Axis and Figure objects do. For example, for Figure, we have backgroundcolor, resolution, font and fontsize as well as the figure_padding which changes the amount of space around the figure content, see the grey area in Figure 5. It can take one number for all sides, or a tuple of four numbers for left, right, bottom and top.

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

Hence, for our next plot we will call several attributes at once as follows:

lines(1:10, (1:10).^2; color=:black, linewidth=2, linestyle=:dash,
backgroundcolor=:grey90, fontsize=16),
axis=(; xlabel="x", ylabel="x²", title="title",
xgridstyle=:dash, ygridstyle=:dash))
current_figure()

This example has already most of the attributes that most users will normally use. Probably, a legend will also be good to have. Which for more than one function will make more sense. 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 6:

lines(1:10, (1:10).^2; label="x²", linewidth=2, linestyle=nothing,
backgroundcolor=:grey90, fontsize=16),
axis=(; xlabel="x", title="title", xgridstyle=:dash,
ygridstyle=:dash))
scatterlines!(1:10, (10:-1:1).^2; label="Reverse(x)²")
axislegend("legend"; position=:ct)
current_figure()

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 the following example illustrates.

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

set_theme!(; resolution=(600, 400),
backgroundcolor=(:orange, 0.5), fontsize=16, font="sans",
Axis=(backgroundcolor=:grey90, xgridstyle=:dash, ygridstyle=:dash),
Legend=(bgcolor=(:red, 0.2), framecolor=:dodgerblue))
lines(1:10, (1:10).^2; label="x²", linewidth=2, linestyle=nothing,
axis=(; xlabel="x", title="title"))
scatterlines!(1:10, (10:-1:1).^2; label="Reverse(x)²")
axislegend("legend"; position=:ct)
current_figure()
set_theme!()

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

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)
xyvals = randn(100, 3)
xyvals[1:5, :]
5×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
-0.340826   0.0589894  -1.76359

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

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

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 5.6.

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? What are all the possible plotting functions available in Makie.jl? To answer this question, a cheat sheet is shown in Figure 9. These work especially well with CairoMakie.jl backend.

For completeness, in Figure 10, we show the corresponding functions cheat sheet for GLMakie.jl, which supports mostly 3D plots. Those will be explained in detail in Section 5.7.

Now, that we have an idea of all the things we can do, let’s go back and continue with the basics. It’s time to learn how to change the general appearance of our plots.

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