# Trying to pass a variable to @df df plot(variable,

**URL:** <https://discourse.julialang.org/t/trying-to-pass-a-variable-to-df-df-plot-variable/105906>\
**Category:** New to Julia\
**Created:** [November 7, 2023, 3:13pm UTC](https://discourse.julialang.org/t/trying-to-pass-a-variable-to-df-df-plot-variable/105906 "2023-11-07T15:13:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [November 7, 2023, 3:13pm UTC](https://discourse.julialang.org/t/trying-to-pass-a-variable-to-df-df-plot-variable/105906/1 "2023-11-07T15:13:31Z")

</div>

I’m trying to get the following code working but can’t figure out what I need to do.

```julia
function plot_type_wealth(dataframe)
    type_vector = [:Supplier, :Consumer]
    label_vector = ["Supplier" "Consumer"]

    @df dataframe plot(type_vector,
        title="Median wealth per actor type\n",
        label=label_vector,
        xlabel="Time",
        ylabel="Median wealth")
end

```

The following piece of code DOES work:

```julia
function plot_type_wealth(dataframe)
    label_vector = ["Supplier" "Consumer"]

    @df dataframe plot([:Supplier, :Consumer],
        title="Median wealth per actor type\n",
        label=label_vector,
        xlabel="Time",
        ylabel="Median wealth")
end

```

The reason I want the first version to work is that I want to pass the columns that need to be plotted as a variable in the function. I know that it has to do with the @df macro but I can’t figure out how to also make it work with the variable.

Thanks in advance,  
Stef

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [November 7, 2023, 3:37pm UTC](https://discourse.julialang.org/t/trying-to-pass-a-variable-to-df-df-plot-variable/105906/2 "2023-11-07T15:37:10Z")

</div>

I think I would just abandon the use of the macro in this case. Does this do what you want?

```julia
using DataFrames
using StatsPlots

df = DataFrame(
	supplier_A=rand(0:20, 30),
	consumer_A=rand(0:20, 30),
	supplier_B=rand(0:20, 30),
	consumer_B=rand(0:20,30)
)

function plot_type_wealth(df, cols)
	plot(
		[df[!, col] for col in cols],
		label=reshape([string(col) for col in cols], 1, length(cols)),
		title="Median wealth per actor type",
		xlabel="Time",
		ylabel="Median wealth"
	)
end

plot_type_wealth(df, [:supplier_A, :consumer_A])

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d8a1c9739fda2608af77e218ad60c449ef7614e.png)

---

<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:** [November 7, 2023, 3:45pm UTC](https://discourse.julialang.org/t/trying-to-pass-a-variable-to-df-df-plot-variable/105906/3 "2023-11-07T15:45:11Z")

</div>

You can’t make this work - the macro gets expanded before runtime and has no access to the value of `type_vector`.

What Matt says is right, if you want to use information on values do that with a regular function, not a macro.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [November 8, 2023, 8:55am UTC](https://discourse.julialang.org/t/trying-to-pass-a-variable-to-df-df-plot-variable/105906/4 "2023-11-08T08:55:04Z")

</div>

Yes! That does the trick.

Thank you!
