# Using Plots.jl within a package \_iteratively\_

**URL:** <https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [January 27, 2020, 3:10pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852 "2020-01-27T15:10:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 27, 2020, 3:10pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/1 "2020-01-27T15:10:19Z")

</div>

Hi, I am doing some optimization iteratively. I produce plots iteratively, updating the plot after each variable-update. I am trying to figure out how to avoid depending on `Plots.jl` in my package for cases when the plot is not requested in order to speed up compile/first-run time. Simplistically, this is like:

```julia
for i in 1:max_iter
  x = update_x(x)
  if make_plot
    plot(x)
  end
end

```

Because the procedure is a bit slow, I want to see the plots appear as the code runs – also produces a nice animation for demo/display.

I have seen the suggestion to use RecipesBase in [Using Plots.jl within a package - #5 by jarvist](https://discourse.julialang.org/t/using-plots-jl-within-a-package/6806/5), but this seems to work only for single static data set. Is there an approach to inject Plots into my optimization code so that it can be called iteratively during optimization?

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [January 28, 2020, 8:35am UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/2 "2020-01-28T08:35:12Z")

</div>

using recipes is orthogonal to if you plot one or many plots. Generally speaking, the thing you have to do, is set up a recipe for whatever type `x` has, using a wrapper type if necessary. Also have a glance at [the documentation](https://docs.juliaplots.org/latest/recipes/).

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 28, 2020, 2:44pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/3 "2020-01-28T14:44:06Z")

</div>

Thank you for the suggestion. I’ve reviewed the docs for RecipiesBase again, but I’m still not clear on how this solves my problem. I want to call the `plot` function from _within_ my package to generate plots as the algorithm is running, on-the-fly. In contrast, it seems that recipies are designed to tell the `plot` function how to plot a new type of data – kinda-like defining a plot method but without requiring the Plots package. Please do correct me if I’m misunderstanding this.

I’m less interested in generating a custom type of plot and more interested in how to just _run_ the plot function from within my package. I’m thinking of just having my optimization function take an optional argument of type `Function` so that I can pass-in `plot` as a way to inject it. Something like

```julia
function optimize(x, plot_function::Union{Function, nothing})
  ...
  for i in 1:max_iter
    x = update_x(x)
    if !isnothing(plot_function)
      plot_function(x)
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 28, 2020, 5:26pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/4 "2020-01-28T17:26:19Z")

</div>

I’m not sure I understand the problem, why can’t you just have the `plot()` call in an if branch and define `optimize(x; plot = false)`?

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 28, 2020, 6:03pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/5 "2020-01-28T18:03:32Z")

</div>

I would like to avoid having Plots.jl as a dependency. It’s only needed for my local debugging, but not for wider use. Having Plots.jl as a dependency slows down compile-time quite a bit.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 28, 2020, 6:07pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/6 "2020-01-28T18:07:15Z")

</div>

Ah sorry it’s all about the dependency - isn’t that then solved by RecipeBase? I.e. depend on `RecipeBase` and define a plot recipe in your package, and then when you want to plot you need to do `using MyPackage, Plots` and if you don’t you can leave it at `using MyPackage`?

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 28, 2020, 6:34pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/7 "2020-01-28T18:34:53Z")

</div>

I would like to `plot` from _within_ a function in my package.

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 28, 2020, 6:36pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/8 "2020-01-28T18:36:41Z")

</div>

Seems like [https://github.com/MikeInnes/Requires.jl](https://github.com/MikeInnes/Requires.jl) is an option for me.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [January 28, 2020, 7:58pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/9 "2020-01-28T19:58:05Z")

</div>

Also havin a callback should work á la

```julia
function f(args...; make_plot = nothing)
    # ...
    if make_plot !== nothing
          make_plot(x)
    end
    # ...
end

```

and then pass `make_plot = Plots.plot`.

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 28, 2020, 8:45pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/10 "2020-01-28T20:45:38Z")

</div>

Awesome, thanks for verifying that my “injection” idea is not crazy 😉 ([Using Plots.jl within a package \_iteratively\_ - #3 by ianfiske](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/3))

This might be the most straightforward solution.

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 30, 2020, 7:50pm UTC](https://discourse.julialang.org/t/using-plots-jl-within-a-package-iteratively/33852/11 "2020-01-30T19:50:24Z")

</div>

Just wanted to circle back and say that passing a `plot_callback` function method works great.
