# How to check if Plots.jl is installed and loaded?

**URL:** <https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047>\
**Category:** Visualization\
**Tags:** package, plotting\
**Created:** [November 13, 2017, 6:11pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047 "2017-11-13T18:11:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 13, 2017, 6:11pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/1 "2017-11-13T18:11:27Z")

</div>

I am writing a function that generates a plot. In case Plots.jl is not installed on the user’s machine or in case it is not loaded in the current Julia session I’d like to print a warning. What is a good way to achieve this?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 13, 2017, 6:23pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/2 "2017-11-13T18:23:13Z")

</div>

`Pkg.installed` and `isdefined(Main, :Plots)` (although the latter will change with 0.7)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 13, 2017, 6:31pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/3 "2017-11-13T18:31:20Z")

</div>

Are you sure that’s a good idea? Why not just put your function in a module, and put `using Plots` in the module? If a user does not have Plots installed, then they will already get an error from Julia, and if they haven’t loaded it then `using Plots` will fix that.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 13, 2017, 6:44pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/4 "2017-11-13T18:44:50Z")

</div>

Thank you @mauro3, could you please explain how the latter will change in Julia v0.7?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 13, 2017, 6:46pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/5 "2017-11-13T18:46:35Z")

</div>

All good suggestions @rdeits, my use case is as follows:

```julia
"""
    VisualComparison

Compare solvers by plotting the results side by side.
"""
struct VisualComparison <: AbstractSolverComparison
  plotspecs
end

VisualComparison() = VisualComparison(nothing)
VisualComparison(; kwargs...) = VisualComparison(kwargs)

function compare(solvers::AbstractVector{S}, problem::AbstractProblem,
                 cmp::VisualComparison) where {S<:AbstractSolver}
  @eval using Plots

  plts = []
  for solver in solvers
    solution = solve(problem, solver)
    push!(plts, plot(solution))
  end

  @show cmp.plotspecs
  plot(plts..., layout=(length(solvers),1))
end

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 13, 2017, 6:47pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/6 "2017-11-13T18:47:47Z")

</div>

> [@juliohm](#):
>
> All good suggestions @rdeits, my use case is as follows:

Seems to me that’s a perfect case for a user recipe.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 13, 2017, 6:48pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/7 "2017-11-13T18:48:42Z")

</div>

How would you reformulate this as a user recipe @ChrisRackauckas? You are probably right 🙂

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 13, 2017, 6:52pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/8 "2017-11-13T18:52:21Z")

</div>

This is an example of a type recipe which plots the series of a bunch of solutions in an internal vector:

[https://github.com/JuliaDiffEq/DiffEqBase.jl/blob/master/src/solutions/monte\_solutions.jl#L94-L102](https://github.com/JuliaDiffEq/DiffEqBase.jl/blob/master/src/solutions/monte_solutions.jl#L94-L102)

I think you just need to use the `@series` macro to build each series as well. That will make them all plot in the same window though, so you’ll need to have @mkborregaard fill in how you then format the plots to be separate windows in that layout you want.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 13, 2017, 6:56pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/9 "2017-11-13T18:56:12Z")

</div>

Thank you @ChrisRackauckas, I am trying to find a solution that is general enough in the sense that not all comparison methods are visual. I am already very happy with the multiple dispatch on the comparison type. I pass in the solvers themselves rather than the solution (data) because some comparisons require calling the solvers multiple times for example (e.g. cross-validation).

For now I think I will just show a warning to the user and redesign this in the future if necessary.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 13, 2017, 7:34pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/10 "2017-11-13T19:34:02Z")

</div>

Related to that code snippet I posted, how one can save the `kwargs` passed to the `VisualComparison(; kwargs)` constructor and use it later in the final plot call? Right now, I am saving the kwargs in a field of type `Any` inside of the VisualComparison object and it is missing the correct type by the time I use the field in the plot command:

```julia
plot(plts..., plotspecs...) # plotspecs from kwargs

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 13, 2017, 9:44pm UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/11 "2017-11-13T21:44:31Z")

</div>

You shouldn’t have to check whether Plots is installed - just write a user recipe like @chrisrackauckas suggests, and use the `subplot` keyword to place the series in the correct subplot (check the MarginalHist recipe here [Recipes · Plots](http://docs.juliaplots.org/latest/recipes/) for how to do it). It will work if the user has Plots loaded and not affect the user’s system otherwise.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [November 14, 2017, 5:09am UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/12 "2017-11-14T05:09:27Z")

</div>

@mkborregaard I already have type recipes defined for the solution type, I only need the function `plot` available inside of the `compare` call. That is why I am checking if Plots.jl is installed, makes sense? This function that I am defining (i.e. compare) is in fact creating a plot out of a recipe that I already have defined in the package.

EDIT:

In other words, I could rename the function `compare` to something like `execute_plots_on_solutions`. Is there a way to achieve this without adding the `@eval using Plots` inside of the function body?

I am tempted to just check if Plots.jl is loaded, and if not, I print a warning.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 14, 2017, 6:02am UTC](https://discourse.julialang.org/t/how-to-check-if-plots-jl-is-installed-and-loaded/7047/13 "2017-11-14T06:02:11Z")

</div>

You can use `RecipesBase.plot` if you just need the `plot` function and wrap the plot call in try-catch, but it’s a bit of a hack.
