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, to 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 setting just do set_theme!() with no arguments. See it in action in the following examples:

function demo_themes()
    Random.seed!(123)
    n = 6
    y = cumsum(randn(n, 10), dims = 2)
    labels = ["$i" for i in 1:n]

    fig, _ = series(y; labels = labels, markersize = 10, color=:Set1,
        axis = (; xlabel = "time (s)", ylabel = "Amplitude",
        title = "Measurements"), figure = (;resolution = (600,300)))
    xh = LinRange(-3,0.5,20)
    yh = LinRange(-3.5,3.5, 20)
    hmap = heatmap!(xh, yh, randn(20,20); colormap = :plasma)
    limits!(-3.1,13,-6,5.1)
    axislegend("legend"; merge = true)
    Colorbar(fig[1,2], hmap)
    fig
end
with_theme(demo_themes, theme_dark())
    with_theme(demo_themes, theme_black())
    with_theme(demo_themes, theme_ggplot2())
    with_theme(demo_themes, theme_minimal())
    with_theme(demo_themes, theme_light())
Figure 11: Theme dark.
Figure 12: Theme black.
Figure 13: Theme ggplot2.
Figure 14: Theme minimal.
Figure 15: Theme light.

Or, defining a custom Theme by doing with_theme(your_plot_function, 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)
    )
function plot_with_legend_and_colorbar()
    n = 15
    x = LinRange(6,9,n)
    y = LinRange(2,5,n)
    m = randn(n,n)

    fig, ax, _ = lines(1:10; label = "line")
    scatter!(1:10; label = "line")
    hm = heatmap!(ax, x, y, m; colormap = :Spectral_11)
    axislegend("legend"; position = :lt,  merge = true)
    Colorbar(fig[1,2], hm, label = "values")
    ax.title = "my custom theme"
    fig
end
with_theme(plot_with_legend_and_colorbar, publication_theme())
Figure 16: Themed plot with Legend and Colorbar.

Now, if something needs to be changed after set_theme!(your_theme), we can do it with update_theme!(resolution = (500,400), fontsize = 18). Another approach will be to pass additional arguments to the with_theme function:

fig = (resolution = (410,400), figure_padding = 1, backgroundcolor= :grey90)
ax = (; aspect = DataAspect())
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.

Now, let’s move on and do a plot with LaTeX strings and a custom theme.



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