# Working with @with, Dataframes and Makie

**URL:** <https://discourse.julialang.org/t/working-with-with-dataframes-and-makie/94039>\
**Category:** New to Julia\
**Tags:** dataframes, glmakie, with\
**Created:** [February 4, 2023, 11:35am UTC](https://discourse.julialang.org/t/working-with-with-dataframes-and-makie/94039 "2023-02-04T11:35:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mreichMPI-BGC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mreichmpi-bgc/32/43775_2.png) [@mreichMPI-BGC](https://discourse.julialang.org/u/mreichMPI-BGC)\
**Post date:** [February 4, 2023, 11:35am UTC](https://discourse.julialang.org/t/working-with-with-dataframes-and-makie/94039/1 "2023-02-04T11:35:20Z")

</div>

I am still trying to figure out best ways of interactive plotting of DataFrames in GLMakie… `AlgebraofGraphics` is nice, but some things can become a bit complicated (cf. [Interactive Menu with AlgebraOfGraphics / GLMakie - #12 by lazarusA](https://discourse.julialang.org/t/interactive-menu-with-algebraofgraphics-glmakie/93842/12))

So I was thinking of the `@with` macro from `StaticModules`

```julia
using StaticModules, GLMakie, DataFrames
x1=rand(50)
y1=10x1.^2
y2=x1 .+ randn(50)
df = DataFrame((;x1, y1, y2))

var=Observable("y1")

fig = @with df begin
    fig=Figure(); ax = Axis(fig[1,1])
    scatter!(ax, x1, @lift(df[!,$var]), color=y2.-y1)
    on(var) do i
        autolimits!(ax)
   end
   fig
end
 
var[]="y2"

```

It works, but repeating the df in the @lift is a bit annoying and I am wondering if this is viable way to go (new to Julia…)  
Thanks!

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [February 4, 2023, 12:34pm UTC](https://discourse.julialang.org/t/working-with-with-dataframes-and-makie/94039/2 "2023-02-04T12:34:35Z")

</div>

I would do a Menu, and put all things lift related before calling the plot (if possible), as in:

```julia
using GLMakie, DataFrames
x1=rand(50)
y1=10x1.^2
y2=x1 .+ randn(50)
df = DataFrame((;x1, y1, y2))

var=Observable("y1")
yv = @lift(df[!,$var])

fig=Figure()
ax = Axis(fig[1,1])
menu = Menu(fig, options = ["y1", "y2"])
scatter!(ax, x1, yv; color = y2.-y1)

fig[1,0] = vgrid!(Label(fig, "Variable", width = nothing), menu;
    tellheight = false, width = 150)
on(menu.selection) do s
    var[] = s
    autolimits!(ax)
end
menu.is_open = true
fig

```

 ![Screenshot 2023-02-04 at 13.33.53](https://global.discourse-cdn.com/julialang/original/3X/6/b/6b8e86faa558b40f619906bc57138197a70c1b17.png)

Note: In your previous approach inside the @with block you are actually creating a new figure each time, and not just updating the previous figure, hence it will lead to bad performance.

---

<div class="post-metadata">

**Author:** ![mreichMPI-BGC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mreichmpi-bgc/32/43775_2.png) [@mreichMPI-BGC](https://discourse.julialang.org/u/mreichMPI-BGC)\
**Post date:** [February 4, 2023, 2:56pm UTC](https://discourse.julialang.org/t/working-with-with-dataframes-and-makie/94039/3 "2023-02-04T14:56:32Z")

</div>

Thanks for the reply - so do you advise against using `@with` at all? Imagine a case where the `df` is coming from “somewhere else”, e.g. a file - then `x1` etc. would not available as variable and one would have to write first

```julia
(; x1, y1, y2) = df

```

and then repeat the varnames also in the `scatter` call.

> [@lazarusA](#):
>
> I would do a Menu

Sure, but that’s a different story…

> [@lazarusA](#):
>
> Note: In your previous approach inside the @with block you are actually creating a new figure each time, and not just updating the previous figure, hence it will lead to bad performance.

Are you sure? `@macroexpand` shows nothing in this direction - mostly just the `df.` is added where appropriate.

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [February 4, 2023, 3:16pm UTC](https://discourse.julialang.org/t/working-with-with-dataframes-and-makie/94039/4 "2023-02-04T15:16:21Z")

</div>

> Imagine a case where the `df` is coming from “somewhere else”

Then I would do the data processing step first and then plot. I’m more in favour of the idea `data comes first`.  
Mixing analysis and plotting in the same workflow is prone to errors, at least for me 😄 . This is a personal preference.

> Are you sure?

```julia
@with df begin
    fig=Figure()

```

only if this is trigger every time there is a new update, not really sure if is activated here 😆 , either way I would do the

```julia
fig=Figure(); ax = Axis(fig[1,1])

```

before, just to be sure, plus I think it will be cleaner.
