# Automatically insert variable name or symbol in Title and Colorbar

**URL:** <https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624>\
**Category:** Visualization\
**Tags:** makie, glmakie\
**Created:** [January 10, 2025, 7:38am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624 "2025-01-10T07:38:56Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 7:38am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/1 "2025-01-10T07:38:56Z")

</div>

I have many different variable names or symbols for different data.  
I will insert one of those variable at a time in my _safe\_log( )_ function. I expect _cbar.label_ and _ax.title_ to accept that variable name automatically rather than changing variable name manually at every place.  
For example i want to insert **Ψ** variable name or symbol in my plot’s Title and color bar.

```julia
using GLMakie
z_log = safe_log.(Ψ)
ax.title = "Ψ variable Plot"
cbar.label = L"log(Ψ)"

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [January 10, 2025, 7:58am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/2 "2025-01-10T07:58:17Z")

</div>

Functions cannot know about the names of the variables passed to them. You either need to write a macro that then passes both name and variable to the function as two separate arguments, or use a dictionary to hold your variables, then you can pass a string or symbol directly and retrieve the data this way.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 8:06am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/3 "2025-01-10T08:06:00Z")

</div>

Please can you explain how to use macro? If i use $ then the values contained in variable name or symbol get printed on plot.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [January 10, 2025, 8:23am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/4 "2025-01-10T08:23:10Z")

</div>

```julia
function m(s::Symbol, value)
    println("The symbol is $s and the value is $value")
end

macro m(variable::Symbol)
    :(m($(QuoteNode(variable)), $(esc(variable))))
end

```

```julia
julia> x = 1
1

julia> @m(x)
The symbol is x and the value is 1

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 9:41am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/5 "2025-01-10T09:41:07Z")

</div>

Sorry, You misunderstood. I have a variable **Ψ** and i will insert its data in `safe_log( )` function. After that i will insert that variable **Ψ** in _cbar.label_ and _ax.title_ to plot symbol **Ψ** in title and color bar.

```julia
safe_log(x::Float64) = x > 0 ? log(x) : 1.0
# Insert variable that you want to plot in z_log function.
z_log = safe_log.(Ψ)

```

---

<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:** [January 10, 2025, 10:00am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/6 "2025-01-10T10:00:25Z")

</div>

You should provide a proper MWE - in your first post you aren’t actually creating a plot at all.

Both your initial post and your reply above seem to suggest that what you’re trying to do is exactly what @jules describes, so if you’re actually trying to do something else you need to be more explicit about what that is.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 10:12am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/7 "2025-01-10T10:12:44Z")

</div>

Please can you give dictionary way example. Thanks.  
I got error while using @m( ) inside another function.

> ERROR: LoadError: UndefVarError: `value` not defined in `Main`  
> Suggestion: check for spelling errors or missing imports.  
> Stacktrace:  
> [1] main(directory::String, N\_r::Int64, N\_θ::Int64, r::Vector{Float64}, θ::Vector{Float64}, μ::Float64, dms::Int64)  
> @ Main ~/Videos/cos\_veloclect.jl:95  
> [2] macro expansion

If i define m function and macro inside my function then i get _local error_.

---

<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:** [January 10, 2025, 10:38am UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/8 "2025-01-10T10:38:21Z")

</div>

```julia
julia> using GLMakie

julia> data = Dict("ψ" => rand(10))
Dict{String, Vector{Float64}} with 1 entry:
  "ψ" => [0.687504, 0.0257956, 0.282592, 0.390958, 0.285716, 0.466667, 0.817611, 0.601683, 0.142801, 0.138936]

julia> function plot_a_variable(varlabel, data_dict)
           lines(data_dict[varlabel], axis = (; title = "A plot of $varlabel"))
       end
plot_a_variable (generic function with 1 method)

julia> plot_a_variable("ψ", data)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/3/534da26ac5ea0ee710c3e03794ce53f45aec8fb2.png)

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 3:01pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/9 "2025-01-10T15:01:50Z")

</div>

Please see this MWE. My data is available at 👇 that earlier post. What modifications should i do in my code so that i have to change variable(here Ψ) only at single place.

> [@Contour plot on Structured grid using Meshes.jl](https://discourse.julialang.org/t/contour-plot-on-structured-grid-using-meshes-jl/123854/1):
>
> [psi.jl](https://discourse.julialang.org/uploads/short-url/27ssQ2AKWVmD6svGyXUwULS6g9w.jl) (103.5 KB)  
> [x.jl](https://discourse.julialang.org/uploads/short-url/jdmXMZCaxdfKpNDOYLq7a2gvBlU.jl) (98.8 KB)  
> [z.jl](https://discourse.julialang.org/uploads/short-url/1b9IkQCrGebxu2S1zmL2KIzxhJi.jl) (97.5 KB)

```julia
using GLMakie, Meshes

x = include("x.jl") # Matrix of x-coordinates
z = include("z.jl") # Matrix of z-coordinates
Ψ = include("psi.jl") # Data matrix
grd = StructuredGrid(x, z)

function m(s::Symbol, value)
    println("The symbol is $s ")
end
macro m(variable::Symbol)
    :(m($(QuoteNode(variable)), $(esc(variable))))
end
@m(Ψ)

safe_log(x::Float64) = x > 0 ? log(x) : 1.0
z_log = safe_log.(Ψ)
fig, ax, plt = Meshes.viz(grd, color=vec(z_log), colorscheme=:jet)
ax.title = "Ψ and Contour Plot (data.$file)"
cbar = Colorbar(fig[1, 2])
cbar.label = L"log(Ψ)"
# Save the plot
save("Demo_Meshes.png", fig)
display(fig)

```

I get this error on using $value.

```julia
julia> z_log = safe_log.($value)

```

> ERROR: syntax: “$” expression outside quote around REPL[10]:1  
> Stacktrace:  
> [1] top-level scope  
> @ REPL[10]:1

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [January 10, 2025, 3:24pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/10 "2025-01-10T15:24:53Z")

</div>

You were meant to transfer the macro technique I showed you to your own function 🙂 if you just call `@m` nothing happens to your own code, it was an example

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 3:29pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/11 "2025-01-10T15:29:14Z")

</div>

I have defined my function for **Ψ** as 👇

```julia
function alfven_surface!(v_x, v_z, B_x, B_z, sinθ, cosθ, vp2, bp2, alfsurf, ρ, tracer)::Array{Float64, 2}
	@. vp2 = v_x^2 + v_z^2
	@. bp2 = B_x^2 + B_z^2
	@. alfsurf = 4.0 * π * ρ * vp2 / bp2
    @inbounds @. alfsurf[(alfsurf .> 1.0) & (tracer .> 0.1)] = 0.0
    return Ψ
end

```

```julia
Ψ = alfven_surface!(v_x, v_z, B_x, B_z, sinθ, cosθ, vp2, bp2, alfsurf, ρ, tracer)

```

so what changes should i do?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [January 10, 2025, 3:52pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/12 "2025-01-10T15:52:34Z")

</div>

It doesn’t matter how the variable is computed, your plotting code is the one that needs changing

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 4:08pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/13 "2025-01-10T16:08:08Z")

</div>

> [@raman\_kumar](#):
>
> `safe_log(x::Float64) = x > 0 ? log(x) : 1.0`

@jules Should i do changes in above or below function? Please explain in detail.

> [@raman\_kumar](#):
>
> `fig, ax, plt = Meshes.viz(grd, color=vec(z_log), colorscheme=:jet)`

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [January 10, 2025, 4:32pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/14 "2025-01-10T16:32:26Z")

</div>

Funnel the variable name into your plotting code so that you can use it in the labels. Do that by wrapping everything in a macro like I showed above

---

<div class="post-metadata">

**Author:** ![simonsteiger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonsteiger/32/209862_2.png) [@simonsteiger](https://discourse.julialang.org/u/simonsteiger)\
**Post date:** [January 10, 2025, 5:04pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/15 "2025-01-10T17:04:45Z")

</div>

Never had a reason to use macros before but I really liked @jules simple example above and wanted to try.

Maybe this would work for you @raman_kumar if you wrap your plotting code similarly?

```julia
using GLMakie

ψ = rand(100)
χ = rand(100)

function m(s::Symbol, values)
	exp_val = exp.(values)
	fig, ax, plt = scatter(exp_val)
	ax.title = "Plot of $s"
	return fig, ax, plt
end

macro m(variable::Symbol)
	:(m($(QuoteNode(variable)), $(esc(variable))))
end

fig1, ax1, plt1 = @m ψ
fig2, ax2, plt2 = @m χ

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 5:18pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/16 "2025-01-10T17:18:15Z")

</div>

> **See this code**
>
> ```julia
> using GLMakie, Meshes
> 
> x = include("x.jl") # Matrix of x-coordinates
> z = include("z.jl") # Matrix of z-coordinates
> Ψ = include("alfsurf.jl") # Data matrix
> grd = StructuredGrid(x, z)
> 
> @m(Ψ)
> safe_log(x::Float64) = x > 0 ? log(x) : 1.0
> function m(s::Symbol, value)
> z_log = safe_log.($value)
> fig, ax, plt = Meshes.viz(grd, color=vec(z_log), colorscheme=:jet)
> ax.title = " $s and Contour Plot"
> cbar = Colorbar(fig[1, 2])
> cbar.label = L"log($s)"
> return fig, ax, plt
> end
> 
> macro m(variable::Symbol)
> :(m($(QuoteNode(variable)), $(esc(variable))))
> end
> fig1, ax1, plt = @m ψ
> 
> # Save the plot
> save("Demo_Meshes.png", fig)
> display(fig)
> 
> ```

gives error after below function code.

```julia
function m(s::Symbol, value)
	z_log = safe_log.($value)
	fig, ax, plt = Meshes.viz(grd, color=vec(z_log), colorscheme=:jet)
	ax.title = " $s and Contour Plot"
	cbar = Colorbar(fig[1, 2])
	cbar.label = L"log($s)"
	return fig, ax, plt
end

```

> ERROR: syntax: “$” expression outside quote around REPL[67]:2  
> Stacktrace:  
> [1] top-level scope  
> @ REPL[67]:1

---

<div class="post-metadata">

**Author:** ![simonsteiger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonsteiger/32/209862_2.png) [@simonsteiger](https://discourse.julialang.org/u/simonsteiger)\
**Post date:** [January 10, 2025, 5:23pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/17 "2025-01-10T17:23:50Z")

</div>

This line

```julia
z_log = safe_log.($value)

```

should not have the `$` since `value` is already an array with the values that you want to call `safe_log()` on.

So I think you should replace that line with:

```julia
z_log = safe_log.(value)

```

Also, in the code you provided above, `save()` and `display()` will error because `fig` is undefined. That should be `fig1`.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [January 10, 2025, 5:38pm UTC](https://discourse.julialang.org/t/automatically-insert-variable-name-or-symbol-in-title-and-colorbar/124624/18 "2025-01-10T17:38:33Z")

</div>

Thank you very much everyone. For me this code worked fine.

```julia
using GLMakie, Meshes

x = include("x.jl") # Matrix of x-coordinates
z = include("z.jl") # Matrix of z-coordinates
Ψ = include("alfsurf.jl") # Data matrix
grd = StructuredGrid(x, z)

safe_log(x::Float64) = x > 0 ? log(x) : 1.0
function m(s::Symbol, value)
	z_log = safe_log.(value)
	fig, ax, plt = Meshes.viz(grd, color=vec(z_log), colorscheme=:jet)
	ax.title = " $s and Contour Plot"
	cbar = Colorbar(fig[1, 2])
	cbar.label = "log($s)"
	return fig, ax, plt
end

macro m(variable::Symbol)
    :(m($(QuoteNode(variable)), $(esc(variable))))
end
fig, ax, plt = @m Ψ

# Save the plot
save("Demo_Meshes.png", fig)
display(fig)

```
