Makie - introduction and snippets

For some time, I am getting more and more enthusiast with the Julia programming language to produce nice visualizations amongst other things. Perhaps the simpler and lighter ecosystem is Plots.jl but I recently turned to the richer Makie.jl ecosystem. It provides nice syntax, tutorials, high quality statics graphics as well as interactive animations.

Of course, I recommend to start with the Getting started tutorial but I also want to feature the Creating complex layouts tutorial which is a gold mine of information. The rest of this blog post is intended for experienced users of Makie.

I recently came up with a canvas for the code I use to produce graphics. This canvas is inspired by the content of the Creating complex layouts tutorial and I use it as a VSCode snippet.

The snippet below creates a canvas for a new Figure with a GridLayout containing an example of animation using GLMakie. You can copy/paste the following code block into your JSON snippet configuration file.

"makie figure": {
    "prefix": "makie figure",
    "body": [
      "# figure initialization",
      "fig = Figure(size=(${1:width}, ${2:height}))",
      "",
      "# GridLayout ${3:gridlayout name}",
      "${3:gridlayout name} = fig[${4:row}, ${5:col}] = GridLayout()",
      "begin # parameters $0",
      "    # sets the parameters of the lines or scatters displayed in the GridLayout",
      "    # they can be Observables to get an animation",
      "    sg = SliderGrid(${3:gridlayout name}[2, 1],",
      "        (label=\"x\", range=-1:0.01:1, startvalue=0),",
      "        tellwidth=false)",
      "    x = sg.sliders[1].value",
      "end",
      "",
      "begin # observables",
      "    # additional Observables defined as functions of the parameters via @lift",
      "    y = @lift \\$x^2",
      "end",
      "",
      "begin # plot layout parameters",
      "    plot_kwargs = (; marker=:rect, alpha=0.9)",
      "end",
      "",
      "begin # axes",
      "    ax = Axis(${3:gridlayout name}[1, 1])",
      "end",
      "",
      "begin # plots",
      "    scatter!(ax, x, y, label=\"parabolic rectangle ?!\"; plot_kwargs...)",
      "end",
      "",
      "begin # legends",
      "    leg = Legend(${3:gridlayout name}[1, 2], ax)",
      "end",
      "",
      "begin # adjust layout",
      "    rowsize!(${3:gridlayout name}, 1, Auto(1.5))",
      "end",
      "",
      "fig"
    ],
    "description": "Canvas for a figure with one gridlayout panel. The canvas is filled with an example using GLMakie which is an animation of a rectangle moving along a parabola."
  }

Furthermore, I use the following snippet to add more GridLayouts.

"makie gridlayout": {
    "prefix": "makie gridlayout",
    "body": [
      "# GridLayout ${1:gridlayout name}",
      "${1:gridlayout name} = fig[${2:row}, ${3:col}] = GridLayout()",
      "begin # parameters $0",
      "    # sets the parameters of the lines or scatters displayed in the GridLayout",
      "    # they can be Observables to get an animation",
      "    sg = SliderGrid(${1:gridlayout name}[2, 1],",
      "        (label=\"x\", range=-1:0.01:1, startvalue=0),",
      "        tellwidth=false)",
      "    x = sg.sliders[1].value",
      "end",
      "",
      "begin # observables",
      "    # additional Observables defined as functions of the parameters via @lift",
      "    y = @lift \\$x^2",
      "end",
      "",
      "begin # plot layout parameters",
      "    plot_kwargs = (; marker=:rect, alpha=0.9)",
      "end",
      "",
      "begin # axes",
      "    ax = Axis(${1:gridlayout name}[1, 1])",
      "end",
      "",
      "begin # plots",
      "    scatter!(ax, x, y, label=\"parabolic rectangle ?!\"; plot_kwargs...)",
      "end",
      "",
      "begin # legends",
      "    leg = Legend(${1:gridlayout name}[1, 2], ax)",
      "end",
      "",
      "begin # adjust layout",
      "    rowsize!(${1:gridlayout name}, 1, Auto(1.5))",
      "end"
    ],
    "description": "Canvas for a gridlayout panel. The canvas is filled with an example using GLMakie which is an animation of a rectangle moving along a parabola."
  }

Finally, I also the snippet below to produce a new Axis.

"makie axis": {
    "prefix": "makie axis",
    "body": [
      "${1:axis name} = Axis(${2:layout name}[${3:row}, ${4:col}],",
      "        title=\"${5:title}\",",
      "        xlabel=\"${6:xlabel}\", ylabel=\"${7:ylabel}\")"
    ],
    "description": "2D axis with custom name, placement, title, xlabel and ylabel"
  }



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Coupling for point processes
  • Popularization of science - Youtube channels