6.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)
Attributes with 14 entries:
  color => black
  colormap => viridis
  colorrange => Automatic()
  inspectable => true
  linestyle => nothing
  linewidth => 1.5
  marker => Circle{T} where T
  markercolor => black
  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 pltobject.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, e.g.:

  lines(x, y)
  lines(x, y, z)

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

  │ Tip
  │  You can separate segments by inserting NaNs.

  lines has the following function signatures:

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

  Available attributes for Lines{T} where T are:


But 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 and the figure_padding which changes the amount of space around the figure content, see the grey area in the plot, Figure (Figure 8). 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,
    figure = (; figure_padding=5, resolution=(600, 400), font="sans",
        backgroundcolor=:grey90, fontsize=16),
    axis = (; xlabel="x", ylabel="x²", title="title",
        xgridstyle=:dash, ygridstyle=:dash))
Figure 8: Custom plot.

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 9:

lines(1:10, (1:10).^2; label="x²", linewidth=2, linestyle=nothing,
    figure = (;figure_padding=5, resolution=(600,400), font="sans",
        backgroundcolor=:grey90, fontsize=16),
    axis = (; xlabel = "x", title = "title", xgridstyle=:dash,
scatterlines!(1:10, (10:-1:1).^2; label="Reverse(x)²")
axislegend("legend"; position=:ct)
Figure 9: Custom plot legend.

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

However, having to write this so much code just for two lines can become cumbersome and tired. 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, not a particular good set of attributes but you’ll get the idea.

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)
Figure 10: set theme

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

Before moving on into the next section, it’s worthwhile to see an example where an array of attributes are 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, here we generated these at random from a normal distribution, Section 7.4.1. Where 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 later 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
xyvals = randn(100, 3)
xyvals[1:5, :]
5×3 Matrix{Float64}:
 -0.294668  1.27304    1.20005
 -0.8177    1.28339   -0.408239
 -0.936189  1.50032    1.65924
 -0.334106  0.395313  -1.4814
  1.81839   0.351304   0.261416

And then the corresponding plot can be seen in Figure 11:

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))
Figure 11: Bubble plot

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.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 12. These work especially well with CairoMakie.jl backend.

Figure 12: Plotting functions: Cheat Sheet. Output given by Cairomakie.

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

Figure 13: Plotting functions: Cheat Sheet. Output given by GLMakie.

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.

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