juliajulia-plotsmakie.jl

Removing grid lines in a CairoMakie plot by setting a custom theme


I would like to set a custom theme for plots using CairoMakie to set xgridvisible and ygridvisible parameters to false for each of my plots.

I tried the following code, but it didn't work.

custom_theme = Theme(ygridvisible = false, xgridvisible = false )
set_theme!(custom_theme)

I do not want to run for every single plot and axes ax.xgridvisible = false


Solution

  • Theming block objects like Axis requires using the (title-case capitalized) type name as a key in the theme. This is explained in the documentation.

    For your specific case, making the x- and y-grids invisible is done like this:

    using CairoMakie
    
    custom_theme = Theme(
        Axis = (
            xgridvisible = false,
            ygridvisible = false,
        ),
    )
    
    function example_plot()
        f = Figure()
        for i in 1:2, j in 1:2
            lines(f[i, j], cumsum(randn(50)))
        end
        Label(f[0, :], "A simple example plot")
        Label(f[3, :], L"Random walks $x(t_n)$")
        f
    end
    

    Without the theme:

    example_plot()
    

    four line plots with grey grid lines in the background of each

    With the theme applied:

    with_theme(custom_theme) do
        example_plot()
    end
    

    four line plots with no grey grid lines in the background of any of them