# Axis-generating functions in Makie

**URL:** <https://discourse.julialang.org/t/axis-generating-functions-in-makie/99558>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [May 29, 2023, 12:31pm UTC](https://discourse.julialang.org/t/axis-generating-functions-in-makie/99558 "2023-05-29T12:31:35Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [May 29, 2023, 2:23pm UTC](https://discourse.julialang.org/t/axis-generating-functions-in-makie/99558/3 "2023-05-29T14:23:30Z")

</div>

Attaching an Axis (or any plot object) to a Figure/Scene after creation is not possible currently.

What I do currently, and which we haven’t had time to consolidate into a real recipe-like API is something like this, where you can plot your recipe into any gridlayout position. If no Figure exists, yet, it is created automatically. It would be nice to have this at some point with full tie-in to the theming system, conversion system, etc.

In this example, you only need to define `compositeplot` once and can reuse it for all plotting functions, then one forwarding method of the plotting function and one implementation method.

There are still things to work out, like what each method should return etc. but it might be a start for you.

```julia
using CairoMakie

function compositeplot(func::Function, gp::Union{GridPosition,GridSubposition}, args...; kwargs...)
    f = Makie.get_top_parent(gp)
    f isa Figure || error("Parent of GridPosition must be a Figure not a $(typeof(f))")
    g = Makie.GridLayoutBase.get_layout_at!(gp; createmissing = true)
    result = func(f, g, args...; kwargs...)
    return f, result
end

function compositeplot(func::Function, args...; figure = (;), kwargs...)
    f = Figure(; figure...)
    result = func(f[1, 1], args...; kwargs...)
    return f, result
end

myplot(args...; kwargs...) = compositeplot(myplot, args...; kwargs...)

function myplot(f::Figure, g::GridLayout, data::Vector{Float64}; colormap = :RdBu)
    ax = Axis(g[1, 1], title = "$colormap")
    sc = scatter!(ax, data; color = randn(length(data)), colormap)
    cb = Colorbar(g[2, 1], sc, vertical = false)
    return (; axis = ax, colorbar = cb, scatterlines = sc)
end

fig, stuff = myplot(cumsum(randn(100)))
myplot(fig[2, 1], cumsum(randn(100)); colormap = :inferno)
_, morestuff = myplot(fig[1:2, 2], cumsum(randn(100)); colormap = :Blues)
morestuff.colorbar.label = "Hi from the label"
fig

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/b/abaa278590729008da922e8f3d718ab8eaffa256.jpeg)

---

_[View the full topic](https://discourse.julialang.org/t/axis-generating-functions-in-makie/99558)._
