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 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 => 12
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.
NaN values are displayed as gaps in the line.
Attributes
============
Specific
––––––––––
• cycle::Vector{Symbol} = [:color] sets which attributes to cycle
when creating multiple plots.
• linestyle::Union{Nothing, Symbol, Vector} = nothing sets the
pattern of the line (e.g. :solid, :dot, :dashdot)
• linewidth::Real = 1.5 sets the width of the line in pixel units.
Generic
–––––––––
• visible::Bool = true sets whether the plot will be rendered or
not.
• overdraw::Bool = false sets whether the plot will draw over other
plots. This specifically means ignoring depth checks in GL
backends.
• transparency::Bool = false adjusts how the plot deals with
transparency. In GLMakie transparency = true results in using
Order Independent Transparency.
• fxaa::Bool = false adjusts whether the plot is rendered with fxaa
(anti-aliasing). Note that line plots already use a different form
of anti-aliasing.
• inspectable::Bool = true sets whether this plot should be seen by
DataInspector.
• depth_shift::Float32 = 0f0 adjusts the depth value of a plot after
all other transformations, i.e. in clip space, where 0 <= depth <=
1. This only applies to GLMakie and WGLMakie and can be used to
adjust render order (like a tunable overdraw).
• model::Makie.Mat4f sets a model matrix for the plot. This replaces
adjustments made with translate!, rotate! and scale!.
• color sets the color of the plot. It can be given as a named color
Symbol or a Colors.Colorant. Transparency can be included either
directly as an alpha value in the Colorant or as an additional
float in a tuple (color, alpha). The color can also be set for
each point in the line by passing a Vector of colors or be used to
index the colormap by passing a Real number or Vector{<: Real}.
• colormap::Union{Symbol, Vector{<:Colorant}} = :viridis sets the
colormap that is sampled for numeric colors.
• colorrange::Tuple{<:Real, <:Real} sets the values representing the
start and end points of colormap.
• nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0) sets a
replacement color for color = NaN.
• space::Symbol = :data sets the transformation space for line
position. See Makie.spaces() for possible inputs.
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,
figure=(; figure_padding=5, resolution=(600, 400), font="sans",
backgroundcolor=:grey90, fontsize=16),
axis=(; xlabel="x", ylabel="x²", title="title",
xgridstyle=:dash, ygridstyle=:dash))
current_figure()
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 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,
figure=(; figure_padding=5, resolution=(600, 400), font="sans",
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()
Figure 6: Custom plot legend.
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!()
Figure 7: Set theme example.
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 6.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
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 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 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 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.
Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso