6.3 Themes
There are several ways to affect the general appearance of your plots. Either, you could use a predefined theme or your own custom theme. For example, use the predefined dark theme via with_theme(your_plot_function, theme_dark())
. Or, build your own with Theme(kwargs)
or even update the one that is active with update_theme!(kwargs)
.
You can also do set_theme!(theme; kwargs...)
to change the current default theme to theme
and override or add attributes given by kwargs
. If you do this and want to reset all previous settings just do set_theme!()
with no arguments. See the following examples, where we had prepared a test plotting function with different characteristics, such that most attributes for each theme can be appreciated.
using Random: seed!
seed!(123)
y = cumsum(randn(6, 6), dims=2)
6×6 Matrix{Float64}:
0.808288 0.386519 0.355371 0.0365011 -0.0911358 1.8115
-1.12207 -2.47766 -2.16183 -2.49928 -2.02981 -1.37017
-1.10464 -1.03518 -3.19756 -1.18944 -2.71633 -3.80455
-0.416993 -0.534315 -1.42439 -0.659362 -0.0592298 0.644529
0.287588 1.50687 2.36111 2.54137 0.48751 0.630836
0.229819 0.522733 0.864515 2.89343 2.06537 2.21375
A matrix of size (20, 20)
with random entries, so that we can plot a heatmap. The range in \(x\) and \(y\) is also specified.
using Random: seed!
seed!(13)
xv = yv = LinRange(-3, 0.5, 20)
matrix = randn(20, 20)
matrix[1:6, 1:6] # first 6 rows and columns
6×6 Matrix{Float64}:
-0.271257 0.894952 0.728865 -0.293849 -0.449277 -0.0948871
-0.193033 -0.421286 -0.455905 -0.0576092 -0.756621 -1.47419
-0.123177 0.762254 0.773921 -0.38526 -0.0659695 -0.599284
-1.47327 0.770122 1.20725 0.257913 0.111979 0.875439
-1.82913 -0.603888 0.164083 -0.118504 1.46723 0.0948876
1.09769 0.178207 0.110243 -0.543203 0.592245 0.328993
Hence, our plotting function looks like follows:
function demo_themes(y, xv, yv, matrix)
fig, _ = series(y; labels=["$i" for i = 1:6], markersize=10,
color=:Set1, figure=(; resolution=(600, 300)),
axis=(; xlabel="time (s)", ylabel="Amplitude",
title="Measurements"))
hmap = heatmap!(xv, yv, matrix; colormap=:plasma)
limits!(-3.1, 8.5, -6, 5.1)
axislegend("legend"; merge=true)
Colorbar(fig[1, 2], hmap)
fig
end
Note that the series
function has been used to plot several lines and scatters at once with their corresponding labels. And since we don’t need the axis neither the plotted object we throw them away with the syntax *_*. Also, a heatmap with their Colorbar
has been included. Currently, there are two dark themes, one called theme_dark()
and the other one theme_black()
, see Figures.
with_theme(theme_dark()) do
demo_themes(y, xv, yv, matrix)
end
with_theme(theme_black()) do
demo_themes(y, xv, yv, matrix)
end
Figure 11: Theme dark.
Figure 12: Theme black.
And three more white-ish themes called, theme_ggplot2()
, theme_minimal()
and theme_light()
. Useful for more standard publication type plots.
with_theme(theme_ggplot2()) do
demo_themes(y, xv, yv, matrix)
end
with_theme(theme_minimal()) do
demo_themes(y, xv, yv, matrix)
end
with_theme(theme_light()) do
demo_themes(y, xv, yv, matrix)
end
Figure 13: Theme ggplot2.
Figure 14: Theme minimal.
Figure 15: Theme light.
Another alternative is defining a custom Theme
by doing with_theme(your_plot, your_theme())
. For instance, the following theme could be a simple version for a publication quality template:
publication_theme() = Theme(
fontsize=16, font="CMU Serif",
Axis=(xlabelsize=20, xgridstyle=:dash, ygridstyle=:dash,
xtickalign=1, ytickalign=1, yticksize=10, xticksize=10,
xlabelpadding=-5, xlabel="x", ylabel="y"),
Legend=(framecolor=(:black, 0.5), bgcolor=(:white, 0.5)),
Colorbar=(ticksize=16, tickalign=1, spinewidth=0.5),
)
Which, for simplicity we use it to plot scatterlines
and a heatmap
.
function plot_with_legend_and_colorbar()
fig, ax, _ = scatterlines(1:10; label="line")
hm = heatmap!(ax, LinRange(6, 9, 15), LinRange(2, 5, 15), randn(15, 15);
colormap=:Spectral_11)
axislegend("legend"; position=:lt)
Colorbar(fig[1, 2], hm, label="values")
colsize!(fig.layout, 1, Aspect(1, 1.0))
ax.title = "my custom theme"
fig
end
Then, using the previously define Theme
the output is shown in Figure (Figure 16).
with_theme(plot_with_legend_and_colorbar, publication_theme())
Figure 16: Themed plot with Legend and Colorbar.
Here we have use with_theme
which is more convenient for the direct application of a theme than the do
syntax. You should use the latter if you want to include extra arguments to the theme that is going to be applied.
Now, if something needs to be changed after set_theme!(your_theme)
, we can do it with update_theme!(resolution=(500, 400), fontsize=18)
, for example. Another approach will be to pass additional arguments to the with_theme
function:
fig = (resolution=(600, 400), figure_padding=1, backgroundcolor=:grey90)
ax = (; aspect=DataAspect(), xlabel=L"x", ylabel=L"y")
cbar = (; height=Relative(4 / 5))
with_theme(publication_theme(); fig..., Axis=ax, Colorbar=cbar) do
plot_with_legend_and_colorbar()
end
Figure 17: Theme with extra args.
Where the x and y labels have a Latex format due to L"..."
. Most basic Latex strings are already supported by Makie, however to fully exploit this integration is recommend to also load the package LaTeXStrings as stated in the next section.
Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso