Custom theme in julia

Basics

Every time julia starts whether it is in interactive mode or when it is used to run scripts, it runs a startup script. It is called startup.jl. Unless otherwise configured it is located at ~/.julia/config/startup.jl

This is a julia script and can contain arbitrary julia code. We can use this script as a configuration file, only now we have more freedom in what can be configured, because we could code to our heart's content, which might not be possible with just a configuration file.

Julia Plots

I still have some quirks about using julia plots, primarily because it still takes a bit of time in the first compilation pass. In a notebook session it is insanely fast compared to python. This is one of the main reason I am heavily gravitated towards julia. Basically prototyping code interactive sessions, e.g., with notebook, is a pleasent experience because the code runs so fast from the second pass onwards.

One of the nice things about julia plots is its ability to use plot attributes in theme. There is a package PlotThemes. It has quite a few nice themes out of the box, but we always want more. It wasn't quite obvious to me at first how to create a custom theme and use it.

In my python scripts when I have to make different versions of the same plot for paper and for slides in presentation, I just switched from different plot themes when using matplotlib for plotting. I wanted something similar with julia as well.

Say in our startup.jl file we have the following content

~/.julia/config
├── themes
│   ├── experiment.jl
│   ├── slide.jl
│   └── theme.jl
└── startup.jl

And in startup.jl we have

include("themes/theme.jl")

I like to organize multiple themes in their own files like, slide.jl , experiment.jl etc and have them available in themes/theme.jl

Lets see the content of themes/slide.jl which is the theme whcih I use to make plots for slide where the font sizes are usually bigger and the canvas size is also bigger.

using PlotThemes, Plots.PlotMeasures
_slide = PlotThemes.PlotTheme(Dict([
    :size => (900,600),
    :framestyle => :box,
    :fontfamily => "Computer Modern",
    :margin => 5mm,
    :grid => true,
    :gridalpha => 0.3,
    :linewidth => 2.5,
    :guidefontsize => 22,
    :titlefontsize => 22,
    :legendfontsize => 20,
    :tickfontsize => 18,
    :minorgrid => true,
    :minorticks => 5,
    :gridlinewidth => 0.6,
    :minorgridalpha => 0.06,
    :legend => :topright,
    :fillalpha => 0.4
    ])
)

To organized all the available themes in a single point, all the themes can be included in the themes/theme.jl file.

using PlotThemes

include("experiment.jl")
include("slide.jl")
include("darkslide.jl")
include("nepali.jl")
include("paper.jl")

PlotThemes._themes[:slide] = _slide
PlotThemes._themes[:darkslide] = _darkslide
PlotThemes._themes[:experiment] = _experiment
PlotThemes._themes[:nepali] = _nepali;
PlotThemes._themes[:paper] = _paper;

It simply reads all the themes that you want to make available. The important magic line that allows the theme to be available in the julia environment is

_themes[:slide]  = _slide

This will put make the plot attributes defined in _slides Dict in themes/slides.jl in the key :slide. This can be used in plots.

Usage

Once the startup.jl file is configured this way. It is very easy to apply themes.

In a julia script

using Plots, LaTeXStrings

theme(:slide) # <-- the theme configured in startup.jl

x = range(0,2π,2000)
y = sin.(2x)

plot(x,y,label=L"\sin(x)")
[FAILED:]>\fig<{sine}

we can simply use the line theme(:slide) to use tht theme.

Now the plot attributes should be applied according the parameters set in ~/.julia/config/theme/slide.jl

Conclusion

It is very easy to create custom themes. The list of configurable parameters in a theme are available in plot attributes section in the documentation.