6.8 A Makie recipe for a DataFrame

Unlike other libraries that already support a wide set of input formats via recipes, i.e. Plots.jl, in Makie.jl most of the time we need to pass the raw data to functions. However, we can also define our own recipe in Makie.jl. A recipe is your own custom plotting type command. This extension is done just in Makie.jl, which means that making a new set of plotting rules for your own types is light, namely, you don’t need the complete plotting machinery available to define them. This is specially useful if you want to include your own plotting commands in one of your own packages. However, in order for them to work you will still need to use one of the backends, i.e., GLMakie or CairoMakie.

As an example we will code a small full recipe for a DataFrame. Please refer to the documentation for more details.

A Makie recipe consist of two parts, a plot type name defined via @recipe and a custom plot!(::Makie.plot) which creates the actual plot via plotting functions already defined.

  1. @recipe(DfPlot, df) do scene
  2. Attributes(
  3. x = :A,
  4. y = :B,
  5. c = :C,
  6. color = :red,
  7. colormap = :plasma,
  8. markersize = 20,
  9. marker = :rect,
  10. colorrange = (0,1),
  11. label = "",
  12. )
  13. end

Note that the macro @recipe will automatically create two new functions for us, dfplot and dfplot!, all lowercase from our type DfPlot. The first one will create a complete new figure whereas the second one will plot into the current axis or an axis of your choosing. This allows us to plot DataFrames which contains columns named, x, y, z. Now, let’s take care of our plot definition. We will do a simple scatter plot:

  1. import Makie
  1. function Makie.plot!(p::DfPlot{<:Tuple{<:DataFrame}})
  2. df = p[:df][]
  3. x = getproperty(df, p[:x][])
  4. y = getproperty(df, p[:y][])
  5. c = getproperty(df, p[:c][])
  6. scatter!(p, x, y; color = c, markersize = p[:markersize][],
  7. colormap = p[:colormap][], marker = p[:marker][],
  8. colorrange = (minimum(x), maximum(c)), label = p[:label][])
  9. return p
  10. end

Note the extras [] at the end of each variable. Those are due to the fact that recipes in Makie are dynamic, meaning that our plots will update if our variables change. See observables to know more. Now, we apply our new plotting function to the following DataFrame:

  1. df_recipe = DataFrame(A=randn(10), B=randn(10), C=rand(10))
  1. fig, ax, obj = dfplot(df_recipe; label = "test")
  2. axislegend()
  3. Colorbar(fig[1,2], obj)
  4. fig

Figure 47: DataFrames recipe.

Figure 47: DataFrames recipe.

The named attributes in the recipe allows us to pass custom names to our new plotting function. Namely:

  1. df_names = DataFrame(a1=rand(100), a2=rand(100), a3=rand(100))

and:

  1. dfplot(df_names; x = :a1, y = :a2, c = :a3, marker = 'o',
  2. axis = (; aspect=1, xlabel = "a1", ylabel = "a2"),
  3. figure = (; backgroundcolor = :grey90))

Figure 48: DataFrames recipe with arguments.

Figure 48: DataFrames recipe with arguments.

Note, that now we are calling by name each column as well as the marker type, allowing us to use this definition for different DataFrames. Additionally, all our previous options, i.e., axis or figure also work!

6.8 A Makie recipe for a Dat.. - 图3 Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso