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.

  1. using Random: seed!
  2. seed!(123)
  3. y = cumsum(randn(6, 6), dims=2)
  1. 6×6 Matrix{Float64}:
  2. 0.808288 0.386519 0.355371 0.0365011 -0.0911358 1.8115
  3. -1.12207 -2.47766 -2.16183 -2.49928 -2.02981 -1.37017
  4. -1.10464 -1.03518 -3.19756 -1.18944 -2.71633 -3.80455
  5. -0.416993 -0.534315 -1.42439 -0.659362 -0.0592298 0.644529
  6. 0.287588 1.50687 2.36111 2.54137 0.48751 0.630836
  7. 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.

  1. using Random: seed!
  2. seed!(13)
  3. xv = yv = LinRange(-3, 0.5, 20)
  4. matrix = randn(20, 20)
  5. matrix[1:6, 1:6] # first 6 rows and columns
  1. 6×6 Matrix{Float64}:
  2. -0.271257 0.894952 0.728865 -0.293849 -0.449277 -0.0948871
  3. -0.193033 -0.421286 -0.455905 -0.0576092 -0.756621 -1.47419
  4. -0.123177 0.762254 0.773921 -0.38526 -0.0659695 -0.599284
  5. -1.47327 0.770122 1.20725 0.257913 0.111979 0.875439
  6. -1.82913 -0.603888 0.164083 -0.118504 1.46723 0.0948876
  7. 1.09769 0.178207 0.110243 -0.543203 0.592245 0.328993

Hence, our plotting function looks like follows:

  1. function demo_themes(y, xv, yv, matrix)
  2. fig, _ = series(y; labels=["$i" for i = 1:6], markersize=10,
  3. color=:Set1, figure=(; resolution=(600, 300)),
  4. axis=(; xlabel="time (s)", ylabel="Amplitude",
  5. title="Measurements"))
  6. hmap = heatmap!(xv, yv, matrix; colormap=:plasma)
  7. limits!(-3.1, 8.5, -6, 5.1)
  8. axislegend("legend"; merge=true)
  9. Colorbar(fig[1, 2], hmap)
  10. fig
  11. 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.

  1. with_theme(theme_dark()) do
  2. demo_themes(y, xv, yv, matrix)
  3. end
  4. with_theme(theme_black()) do
  5. demo_themes(y, xv, yv, matrix)
  6. end

Figure 11: Theme dark.

Figure 11: Theme dark.

Figure 12: Theme black.

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.

  1. with_theme(theme_ggplot2()) do
  2. demo_themes(y, xv, yv, matrix)
  3. end
  4. with_theme(theme_minimal()) do
  5. demo_themes(y, xv, yv, matrix)
  6. end
  7. with_theme(theme_light()) do
  8. demo_themes(y, xv, yv, matrix)
  9. end

Figure 13: Theme ggplot2.

Figure 13: Theme ggplot2.

Figure 14: Theme minimal.

Figure 14: Theme minimal.

Figure 15: Theme light.

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:

  1. publication_theme() = Theme(
  2. fontsize=16, font="CMU Serif",
  3. Axis=(xlabelsize=20, xgridstyle=:dash, ygridstyle=:dash,
  4. xtickalign=1, ytickalign=1, yticksize=10, xticksize=10,
  5. xlabelpadding=-5, xlabel="x", ylabel="y"),
  6. Legend=(framecolor=(:black, 0.5), bgcolor=(:white, 0.5)),
  7. Colorbar=(ticksize=16, tickalign=1, spinewidth=0.5),
  8. )

Which, for simplicity we use it to plot scatterlines and a heatmap.

  1. function plot_with_legend_and_colorbar()
  2. fig, ax, _ = scatterlines(1:10; label="line")
  3. hm = heatmap!(ax, LinRange(6, 9, 15), LinRange(2, 5, 15), randn(15, 15);
  4. colormap=:Spectral_11)
  5. axislegend("legend"; position=:lt)
  6. Colorbar(fig[1, 2], hm, label="values")
  7. colsize!(fig.layout, 1, Aspect(1, 1.0))
  8. ax.title = "my custom theme"
  9. fig
  10. end

Then, using the previously define Theme the output is shown in Figure (Figure 16).

  1. with_theme(plot_with_legend_and_colorbar, publication_theme())

Figure 16: Themed plot with Legend and Colorbar.

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:

  1. fig = (resolution=(600, 400), figure_padding=1, backgroundcolor=:grey90)
  2. ax = (; aspect=DataAspect(), xlabel=L"x", ylabel=L"y")
  3. cbar = (; height=Relative(4 / 5))
  4. with_theme(publication_theme(); fig..., Axis=ax, Colorbar=cbar) do
  5. plot_with_legend_and_colorbar()
  6. end

Figure 17: Theme with extra args.

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.

6.3 Themes - 图8 Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso