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)
pltobj.attributes
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.:

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{T} where T are:

    ambient
    color
    colormap
    colorrange
    cycle
    diffuse
    inspectable
    lightposition
    linestyle
    linewidth
    nan_color
    overdraw
    shininess
    specular
    ssao
    transparency
    visible

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

figure = (; figure_padding=5, resolution=(600,400),
    backgroundcolor=:grey90, fontsize=16, font="sans")

axis = (; xlabel="x", ylabel="x²", title="title",
    backgroundcolor=:white, xgridstyle=:dash, ygridstyle=:dash)

lines(1:10, (1:10).^2; color=:black, linewidth=2, linestyle=:dash, figure, axis)
Figure 5: 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 (Figure 6):

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

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.

set_theme!(resolution = (600,400), backgroundcolor = (:orange, 0.5),
    fontsize = 16, font = "sans",
    Axis = (backgroundcolor = :white, 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!() # in order to go back to the default settings.
Figure 7: set theme

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.

let
    Random.seed!(123)
    n = 100
    fig, ax, pltobj = scatter(randn(n), randn(n); color = randn(n),
        label = "Bubbles",colormap = :plasma, markersize = 25*rand(n),
        figure = (; resolution = (550,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
end
Figure 8: 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 plot 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 9. These work especially well with CairoMakie.jl backend.

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

For completeness, in Figure 10 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 10: 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.



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