Using Plots.jl within a package

I have a little computational physics code written as a Julia package, which includes some helper ‘plot’ functions to generate (output) figures.

However, just using Plots in the package preamble is enough to seriously slow down the use of the code when e.g. running tests, or just getting numeric results.
If I don’t have a ‘using’ statement, the relevant functions fail to find the ‘plot’ command, even when using Plots is explicitly imported into the script.

Is there any sensible way to deal with this (occasional, optional) dependency? Should I separate the plotting code (with a using Plots dependency) into a separate sub-Module?


Don’t depend on Plots.jl. Depend on RecipesBase and write recipes.


Certainly not a bad idea.

Is there no way to escape having to Using FOO at the granularity of the entire package?

In the short term I have split off a ‘plot’ mini-package, which has the Using Plots dependency. This seems to work well enough for my needs, and would allow future deprecation + replacement with a RecipesBase recipe.

There is not, which is why packages which are large and expect people to build dependencies have a way out, which for Plots.jl is recipes. You shouldn’t avoid them: they were built for this purpose.

1 Like

Okey doke! Your replies were very clear and immediately useful, thank you.


No problem. I’ll link the recipes page since I haven’t yet:

and note that if you need help with writing recipes (they can be a little confusing at first, but once you understand it you’ll see it’s very similar to just writing a plot command in disguise), just stop by the chatroom: