# Makie plot specification?

**URL:** https://discourse.julialang.org/t/makie-plot-specification/116979
**Category:** Visualization
**Tags:** makie
**Created:** [July 12, 2024, 6:56pm UTC](https://discourse.julialang.org/t/makie-plot-specification/116979 "2024-07-12T18:56:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)
#### Post date: [July 12, 2024, 6:56pm UTC](https://discourse.julialang.org/t/makie-plot-specification/116979/1 "2024-07-12T18:56:04Z")

</div>

I have a package, call it MyModels.jl, that needs to be light and fast, used mostly in backend tools that don’t want any visualization stuff in their dependencies. The package implements some mathematical models. In _some_ use cases, we need to plot the results of those models, and how each model should be plotted is very much custom for that model. Right now, we have a separate package, MyModelsPlots.jl, that adds on the plotting functionality. Downstream users that want visualization can add this package to their projects. That works, but keeping the two packages tied together is tedious.

I was wondering if there was (or maybe ought to be some day) a package to allow a package like MyModels.jl to specify how to construct its plots, but that doesn’t actually _do_ any plotting or depend on all the heavy visualization backends. That is, maybe there’s a package that’s _just_ how you tell Makie what you want plotted.

Here’s an example. Please ignore that this doesn’t look like the Makie interface; it’s more like pseudocode.

```julia
module MyModels

using MakieSpecification

# Let's define ModelA's structure and behavior.
struct ModelA
    ... # Whatever
end

# Here's how ModelA should be plotted.
function plot_model(m::ModelA)
    return MakieSpecification.PlotSpec(
        figure = MakieSpecification.FigureSpec(
            axes = [
                MakieSpecification.AxisSpec(
                    plots = [
                        MakieSpecification.LineSpec(m.t, m.x),
                    ],
                    xlabel = "Time",
                    ylabel = "Model A: x",
                ),
            ]
        )
    )
end

```

The end user would then just do something like this:

```julia
using MyModels
import GLMakie

a = MyModels.ModelA()
# Do something with the model.
# ...

# Let's get the plot specification for the model and let GLMakie actually realize the plot.
GLMakie.plot(MyModels.plot_model(a))

```

Just to be super clear here, the key is that the same lightweight package that defines the structure and functionality would also describe _how_ it is to be plotted, without it depending on the code that _does_ the plotting. It would only depend on a plot specification package of some kind. This would let light users who don’t need plots to skip those dependencies but users who want them could have them without having a whole separate codebase to try to use/update in parallel.

Does this exist? Could it? Should it?

---

<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: [July 12, 2024, 11:06pm UTC](https://discourse.julialang.org/t/makie-plot-specification/116979/2 "2024-07-12T23:06:17Z")

</div>

One could imagine some sort of Dict interface to the SpecApi that Makie already has. But nowadays it’s much easier to take on a weak dependency in this case. Then you can write your code with Makie’s full API, don’t have to write a separate package really, and users don’t pay the cost unless Makie is installed explicitly.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 12, 2024, 11:09pm UTC](https://discourse.julialang.org/t/makie-plot-specification/116979/3 "2024-07-12T23:09:16Z")

</div>

> [@tuckermcclure](#):
>
> Does this exist? Could it? Should it?

it doesn’t. But the correct way to achieve the same functionality is to use Extention mechanism of Pkg.jl: [5. Creating Packages · Pkg.jl](https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions))

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [July 13, 2024, 10:35am UTC](https://discourse.julialang.org/t/makie-plot-specification/116979/4 "2024-07-13T10:35:47Z")

</div>

Yeah it doesn’t exist yet, but was planned for Plot serialization and dependency free recipes (which I agree isn’t as much needed anymore with extensions).  
If you really want this, I encourage you to prototype a Dict based, easy to serialize version of the plotspec, with proper translation functions in a package with:

```julia
dict_to_spec(spec::Dict)::PlotSpec
spec_to_dict(spec::PlotSpec)::Dict

```

---

<div class="post-metadata">

### Author: ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)
#### Post date: [July 15, 2024, 4:57pm UTC](https://discourse.julialang.org/t/makie-plot-specification/116979/5 "2024-07-15T16:57:50Z")

</div>

Thanks, all! It sounds like I have some things here to look into. I didn’t know about the SpecApi, so that seems like the place for me to start. I’ll come back to this thread with my ideas to see if they’re useful.

---

<div class="post-metadata">

### Author: ![tuckermcclure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tuckermcclure/32/1187_2.png) [@tuckermcclure](https://discourse.julialang.org/u/tuckermcclure)
#### Post date: [March 14, 2025, 4:10am UTC](https://discourse.julialang.org/t/makie-plot-specification/116979/6 "2025-03-14T04:10:03Z")

</div>

> [@jling](#):
>
> But the correct way to achieve the same functionality is to use Extention mechanism

Yes, thanks. Just to follow up, we’ve been using a Makie extension for the package in question, and that works quite well.

I still sort of like the idea of a simple dictionary to go to a plot specification using Makie’s PlotSpec type, simply because I find the PlotSpec easy to use for simple plots, but it’s really not required for this use case.
