# Recipes distinguishing plot! from plot, for Plots.jl

**URL:** <https://discourse.julialang.org/t/recipes-distinguishing-plot-from-plot-for-plots-jl/11143>\
**Category:** Visualization\
**Tags:** plots, recipe\
**Created:** [May 25, 2018, 12:09pm UTC](https://discourse.julialang.org/t/recipes-distinguishing-plot-from-plot-for-plots-jl/11143 "2018-05-25T12:09:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [May 25, 2018, 12:09pm UTC](https://discourse.julialang.org/t/recipes-distinguishing-plot-from-plot-for-plots-jl/11143/1 "2018-05-25T12:09:19Z")

</div>

As the title says, is there a way for a recipe to distinguish whether it was called from `plot!()` rather than `plot()`?

For a trivial example, suppose `p = plot(x::MyType)` works out limits for axes, and normalises somehow, and that I want `plot!(y)` to add a few data points onto that, without changing axis scales, and re-using the same normalisation. Which I’d be happy to save as a global state somewhere, although if it were passed along as `plot!(p, y)` that would be nicer.

I believe what I have is called a “type recipe”. And that “user recipes” define new function `myplot(x)`, which automatically comes with a related `myplot!(x)` cousin, so won’t let me do this.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [May 25, 2018, 8:15pm UTC](https://discourse.julialang.org/t/recipes-distinguishing-plot-from-plot-for-plots-jl/11143/2 "2018-05-25T20:15:07Z")

</div>

Distinguishing `plot` and `plot!` directly is not possible. One option is to pass around a state like so:

```julia
using Plots
using RecipesBase

struct MyType 
    xs::Vector{Float64}
    ys::Vector{Float64}
end
mutable struct MyState 
    state::Int
end
MyState(t::MyType) = MyState(0)

@recipe function plot(s::MyState, t::MyType)
    # some complicated logic that fiddles with the state
    s.state += 1
    label --> string(s)
    t.xs, t.ys
end

t = MyType(randn(10),randn(10))
s = MyState(t)
t2 = MyType(randn(10), randn(10))
plot(s,t)
scatter!(s, t2)

```
