Are recipes still an "advanced topic"?

I was recently trying to figure out how to let users of my code make some plots, without making Plots.jl (and its hefty list of dependencies) a dependency of the package.

Recipes seem like they’re aimed at this use case.

I found this thread from 2017: Understanding recipes in Plots.jl - #12 by tbreloff

I’m indeed finding that to be true (I couldn’t figure out how to make a working recipe), but this also seems to contradict the first sentence in the Recipes docs themselves:

Recipes are a way of defining visualizations in your own packages and code, without having to depend on Plots.

Maybe some questions I have are (though I don’t exactly know what to ask):

  1. Do I need to clump everything I want to plot into a single object, which then uses a single plot() function? Or can a recipe be made of multiple calls to plot(), plot!(), annotate!(), etc. to add elements?
  2. Is there an example somewhere of taking existing plot code and converting it to a recipe? I’m not grasping some basic concepts, I’m sure.
  3. Does the user need to be able to call plot() directly, or could I have a function in my package that does it? My use case is to make one plot each time through a loop (to inspect progress) but there aren’t currently any hooks in that loop for the user to intervene.

We have package extendions now which allows conditional code loading depending if other packages are loaded:

This has been supported since Julia 1.9, meaning the the current LTS Julia 1 20 release supports this.

This means you can put plotting code in extensions for Plots, Makie, or even UnicodePlots.

5 Likes

Does How do Recipes actually work? help?

3 Likes

I‘ve found it useful to search for (SomePackage)RecipesBaseExt on GitHub and look at what others do to allow their custom data models to be plotted.

3 Likes

That may be more suitable than recipes in my case, and require a heck of a lot less machinery - I’ll have a look, thanks.

I recently bit the bullet to learn how to write these recipes, and once I got it down I find it to be a very useful skill. The syntax did, admittedly, feel a little bit arcane at first, and the differing recipe types made it hard for me to put together exactly what I wanted to do–the suggestion to look at other recipes in extensions is a good one, and frankly you can just start with the “user recipes” and get most of the functionality, then gradually add in the other kinds once you have a handle on it.

I haven’t figured out all the subtleties like annotations yet, but even so the recipe system makes things much better.

The biggest difference for me was when I make lots of similar plots with slight but important variations–without recipes, all the variations would end up being a bunch of keyword arguments, and I frequently had to add new ones. With recipes, though, all of the basic Plots.jl keyword arguments work with every recipe call, and with some careful design of which separate series should be separate recipes, my plots became significantly easier to maintain, and I don’t have to change the recipes nearly so often.

My own code base might have some examples where I did this myself, which if you are still interested I could turn into something resembling an MWE with and without recipes.

2 Likes