6.5 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 demo_figure with different characteristics, such that most attributes for each theme can be appreciated.

Our demo_figure plotting function looks like follows:

function demo_figure()
    # contourf
    x=y= range(-2.3, 2.3, length=100)
    z=-2x .* exp.(-x .^ 2 .- (y') .^ 2)
    # density
    μσpairs=[[4,2],[2,0.5],[-1,2],[0.25,1],[-4, 2],[8, 1.5]]
    # lines
    xs=range(0, 4π, 100)
    ys=[sin.(xs),cos.(xs),-sin.(xs),-cos.(xs),0.5cos.(xs),-0.5cos.(xs)]
    labs=["sin(x)","cos(x)","-sin(x)","-cos(x)","cos(x)/2","-cos(x)/2"]
    # figure
    fig = Figure(figure_padding=(10,15,5,35),
        size = (900,600), fontsize = 20)
    ax1 = Axis(fig[1,1]; xlabel="x", ylabel="y")
    ax2 = Axis(fig[1,2]; xlabel="x", ylabel="pdf")
    ax3 = Axis(fig[2,1:2]; xlabel="x", ylabel="y")
    obj = contourf!(ax1, x,y,z; levels=30)
    Colorbar(fig[1,1, Top()], obj;
        vertical=false,
        tellheight=false,
        tellwidth=false,
        valign=:top
        )
    for μσ in μσpairs
        density!(ax2, rand(Distributions.Normal(μσ...),1000);
            label="$(μσ[1]),$(μσ[2])")
    end
    Legend(fig[1,2, Top()], ax2;
        orientation=:horizontal,
        nbanks=2,
        backgroundcolor =:transparent,
        tellheight=false,
        valign=:center,
        )
    limits!(ax2, -10,15,0,1)
    [lines!(ax3, xs, y, label = "$(labs[i])") for (i,y) in enumerate(ys)]
    limits!(ax3, 0,4π,-1,1)
    Legend(fig[2,1:2, Top()], ax3, orientation=:horizontal)
    fig
end

Where we had use several keyword arguments for Axis, Legend and Colorbar, please play with them and see how they work.

Currently, there are five themes:

  • theme_dark()
  • theme_black()
  • theme_ggplot2()
  • theme_minimal()
  • theme_light()
Figure 13: Predefined themes.

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,xlabelpadding=-5,
        xgridstyle=:dash, ygridstyle=:dash,
        xtickalign=1, ytickalign=1,
        yticksize=10, xticksize=10,
        xlabel="x", ylabel="y"
        ),
    Legend=(framecolor=(:black, 0.5), backgroundcolor=(: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, range(6, 9, 15), range(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 defined Theme the output is shown in Figure (Figure 14).

with_theme(plot_with_legend_and_colorbar, publication_theme())
Figure 14: 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!(size=(500, 400), fontsize=18), for example. Another approach will be to pass additional arguments to the with_theme function:

fig = (size=(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 15: 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