# How to bind data with plot function?

**URL:** <https://discourse.julialang.org/t/how-to-bind-data-with-plot-function/76164>\
**Category:** New to Julia\
**Tags:** question, plots, r, recipe\
**Created:** [February 10, 2022, 1:52pm UTC](https://discourse.julialang.org/t/how-to-bind-data-with-plot-function/76164 "2022-02-10T13:52:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dankelley/32/33539_2.png) [@dankelley](https://discourse.julialang.org/u/dankelley)\
**Post date:** [February 10, 2022, 1:52pm UTC](https://discourse.julialang.org/t/how-to-bind-data-with-plot-function/76164/1 "2022-02-10T13:52:42Z")

</div>

This is my first posting here, and I apologize if this is silly.

I am quite familiar with R, and a scheme I use there is to compute something and assign it a class, so that I can then define a plot generic. You can see that below. The idea of this is that the `foo` and `plot.foo` functions would normally go in a library, so that the user simply has to call that library and then execute the last two lines in my example.

My question is how this ought to be done in Julia. I sort of think what I want to do is to return a named tuple that perhaps holds `x`, `y` and a function named `plot` that does the plotting. Is that the recommended approach? I’m hoping to learn to write Julia in a way that will not just work, but also be familiar to users, and that means I’m hoping that someone can advise me on what is the “Julia way”.

```julia
# R code
foo <- function(x)
{
    res <- list(x=x, y=sin(x))
    class(res) <- "Foo"
    res
}
plot.Foo <- function(x)
{
    plot(x$x, x$y)
}

s <- foo(seq(0, 2*pi, pi/64))
plot(s)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [February 10, 2022, 2:10pm UTC](https://discourse.julialang.org/t/how-to-bind-data-with-plot-function/76164/2 "2022-02-10T14:10:11Z")

</div>

I think you want something like this, which is creating a overload of the plotting function for a specific type you create (here exemplified with UnicodePlots):

```julia
julia> using UnicodePlots
       struct Foo
           x::Vector{Float64}
           y::Vector{Float64}
       end
       Foo(x) = Foo(x,sin.(x))
       plot(f::Foo) = lineplot(f.x,f.y)
plot (generic function with 1 method)

julia> f = Foo(0:pi/64:2*pi)
Foo([0.0, 0.04908738521234052, 0.09817477042468103, 0.14726215563702155, 0.19634954084936207, 0.2454369260617026, 0.2945243112740431, 0.3436116964863836, 0.39269908169872414, 0.44178646691106466 … 5.841398840268521, 5.890486225480862, 5.939573610693203, 5.988660995905543, 6.037748381117884, 6.086835766330224, 6.135923151542564, 6.1850105367549055, 6.234097921967246, 6.283185307179586], [0.0, 0.049067674327418015, 0.0980171403295606, 0.14673047445536175, 0.19509032201612825, 0.24298017990326387, 0.29028467725446233, 0.33688985339222005, 0.3826834323650898, 0.4275550934302821 … -0.42755509343028253, -0.3826834323650904, -0.33688985339222, -0.2902846772544625, -0.24298017990326418, -0.19509032201612872, -0.1467304744553624, -0.0980171403295605, -0.04906767432741809, -2.4492935982947064e-16])

julia> plot(f)
      ┌────────────────────────────────────────┐ 
    1 │⠀⠀⠀⠀⠀⠀⡤⠊⠉⠉⠑⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⢀⠞⠀⠀⠀⠀⠀⠀⠳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⢠⠃⠀⠀⠀⠀⠀⠀⠀⠀⠱⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⢀⠎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠱⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⢀⠞⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⡜⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⣰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠧⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠼⡤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⠤⢤⠤⠤⠤⠤│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠇⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡜⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡴⠁⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⡄⠀⠀⠀⠀⠀⠀⠀⠀⡰⠁⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢦⠀⠀⠀⠀⠀⠀⡴⠁⠀⠀⠀⠀⠀⠀⠀⠀│ 
   -1 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠳⢄⣀⣀⡠⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      └────────────────────────────────────────┘ 
      ⠀0⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀7⠀ 

julia> 

```

You can also make the evaluation lazy, for example:

```julia

julia> struct Foo2{T}
           r::T
       end

julia> plot(f::Foo2) = lineplot(f.r,sin.(f.r))
plot (generic function with 1 method)

julia> plot(f2)
      ┌────────────────────────────────────────┐ 
    1 │⠀⠀⠀⠀⠀⠀⡤⠊⠉⠉⠑⢤⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
      │⠀⠀⠀⠀⢀⠞⠀⠀⠀⠀⠀⠀⠳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀│ 
...

```

then the structure only stores the step range, not the actual data.

---

<div class="post-metadata">

**Author:** ![dankelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dankelley/32/33539_2.png) [@dankelley](https://discourse.julialang.org/u/dankelley)\
**Post date:** [February 10, 2022, 2:22pm UTC](https://discourse.julialang.org/t/how-to-bind-data-with-plot-function/76164/3 "2022-02-10T14:22:32Z")

</div>

Thanks so much for this, @Imiq. Not only did you answer my question quickly and clearly, but you also taught me about UnicodePlots, which is very handy. I really appreciate the help.

---

<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:** [February 10, 2022, 2:24pm UTC](https://discourse.julialang.org/t/how-to-bind-data-with-plot-function/76164/4 "2022-02-10T14:24:58Z")

</div>

I guess the main difference in Julia is that there is no built-in plotting functionality, so you can’t just call `plot` unless you’ve loaded a package that does the plotting.

How exactly you’d implement what you’re after then depends on the plotting package you’re using. One potential issue with this is that you might have to add the plotting package as a dependency of your package, which is often quite heavy. One way around that is to use plot recipes, defined in the RecipesBase package:

[https://github.com/JuliaPlots/RecipesBase.jl](https://github.com/JuliaPlots/RecipesBase.jl)

This allows you to just add the very lightweight `RecipesBase` dependency to your package, and then the user can do `using YourPackage, Plots; plot(object_of_your_type)` if they want to plot, but don’t have to deal with the large `Plots` dependency if they want to use the package without plotting.
