# Is there a function to name generated plot or rename variable?

**URL:** https://discourse.julialang.org/t/is-there-a-function-to-name-generated-plot-or-rename-variable/40064
**Category:** General Usage
**Tags:** question, plotting
**Created:** [May 24, 2020, 10:42am UTC](https://discourse.julialang.org/t/is-there-a-function-to-name-generated-plot-or-rename-variable/40064 "2020-05-24T10:42:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [May 24, 2020, 10:42am UTC](https://discourse.julialang.org/t/is-there-a-function-to-name-generated-plot-or-rename-variable/40064/1 "2020-05-24T10:42:43Z")

</div>

Hi,

I’d like to naming plot or variables in a for loop, interpolating corresponding id to their name, so later on I can use them in manner of my combination and order.

I checked related functions by `?name` but haven’t found what I want. Any suggestion?

```julia
for i in 1:5
    p=plot(rand(10))
    name(p)="p$i" #are there functions in julia can achieve this? 
end

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [May 24, 2020, 11:00am UTC](https://discourse.julialang.org/t/is-there-a-function-to-name-generated-plot-or-rename-variable/40064/2 "2020-05-24T11:00:07Z")

</div>

I think it is better to put the plot into an array:  
E.g if you use the default backend (GR):

```julia
allPlots=Array{Plots.Plot{Plots.GRBackend},1}(undef,5)
for i in 1:5
    allPlots[i]=plot(rand(10))
end

```

But if you insist in different variables for each plot you can use eval:

```julia
for i in 1:5
    p="p$i=plot(rand(10))"
    eval(Meta.parse(p))
end

```

Another way would be creating your own macro.  
[https://docs.julialang.org/en/v1/manual/metaprogramming/index.html#man-macros-1](https://docs.julialang.org/en/v1/manual/metaprogramming/index.html#man-macros-1)

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [May 24, 2020, 11:51am UTC](https://discourse.julialang.org/t/is-there-a-function-to-name-generated-plot-or-rename-variable/40064/3 "2020-05-24T11:51:54Z")

</div>

Thank you~ The fact that array can contain plots actually suprised me. Can array also contain dataframe like:

> Array{DataFrames.DataFrame{Float64,2}, 1}(undef, 2,3,2)

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [May 24, 2020, 12:03pm UTC](https://discourse.julialang.org/t/is-there-a-function-to-name-generated-plot-or-rename-variable/40064/4 "2020-05-24T12:03:23Z")

</div>

Sure, but it would be:

```julia
julia> aOfDf=Array{DataFrame}(undef,5)
5-element Array{DataFrame,1}:
 #undef
 #undef
 #undef
 #undef
 #undef

julia> aOfDf[1]=DataFrame( [Float64,Float64],[:x,:y],3)
3×2 DataFrame
│ Row │ x │ y │
│ │ Float64 │ Float64 │
├─────┼─────────┼──────────────┤
│ 1 │ 0.0 │ 5.51928e-315 │
│ 2 │ 0.0 │ 8.0953e-316 │
│ 3 │ 0.0 │ 5.51928e-315 │

julia> aOfDf[2]=DataFrame( [Float64,Float64,Float64],[:x,:y,:z],3)
3×3 DataFrame
│ Row │ x │ y │ z │
│ │ Float64 │ Float64 │ Float64 │
├─────┼──────────────┼──────────────┼──────────────┤
│ 1 │ 1.16653e-315 │ 1.16653e-315 │ 1.13457e-315 │
│ 2 │ 6.91943e-316 │ 1.16653e-315 │ 6.91942e-316 │
│ 3 │ 6.92265e-316 │ 6.91943e-316 │ 6.91942e-316 │

julia> aOfDf
5-element Array{DataFrame,1}:
    3×2 DataFrame
│ Row │ x │ y │
│ │ Float64 │ Float64 │
├─────┼─────────┼──────────────┤
│ 1 │ 0.0 │ 5.51928e-315 │
│ 2 │ 0.0 │ 8.0953e-316 │
│ 3 │ 0.0 │ 5.51928e-315 │
    3×3 DataFrame
│ Row │ x │ y │ z │
│ │ Float64 │ Float64 │ Float64 │
├─────┼──────────────┼──────────────┼──────────────┤
│ 1 │ 1.16653e-315 │ 1.16653e-315 │ 1.13457e-315 │
│ 2 │ 6.91943e-316 │ 1.16653e-315 │ 6.91942e-316 │
│ 3 │ 6.92265e-316 │ 6.91943e-316 │ 6.91942e-316 │
 #undef
 #undef
 #undef

```

So, you don’t have to specify the DataFrame for the Array, it’s just any DataFrame.
