5.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}:
  1.19027    2.17124    1.2823     0.56489    0.842615  -0.0284417
  2.04818    1.9727     2.29991    1.52484    3.04876    3.39449
  1.14265    1.41647    2.00887    1.02933   -0.748397  -0.904251
  0.459416   0.265187   0.633189   0.893591  -2.03947    0.293544
 -0.396679  -0.736045  -1.01718   -1.48567   -0.70341   -1.70176
 -0.664713  -1.50859   -2.24348   -3.12437   -0.810794  -0.743838

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.410261    0.755685   1.84697    0.0645615   1.53034      0.557182
 -2.06413     1.2311     0.330232  -0.793936   -1.10552     -2.1084
  0.0749639  -3.42235    0.392709   2.47305    -1.10597      0.962373
  0.803344    1.78866   -0.81155   -1.70707     0.00106256   0.297236
 -1.24842     1.4372     1.11774   -0.952159   -0.0887516    0.0106082
 -1.43937     1.31755    0.631643   0.261686   -0.402386     0.233161

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. 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")
    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.

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.

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, Lazaro Alonso