# Annotated unitful axis in Plots

**URL:** <https://discourse.julialang.org/t/annotated-unitful-axis-in-plots/103732>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [September 11, 2023, 8:21am UTC](https://discourse.julialang.org/t/annotated-unitful-axis-in-plots/103732 "2023-09-11T08:21:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [September 11, 2023, 8:21am UTC](https://discourse.julialang.org/t/annotated-unitful-axis-in-plots/103732/1 "2023-09-11T08:21:22Z")

</div>

I’m plotting a function, which accepts a unitful argument. Now, if I want to add an x axis label

```julia
p = plot!(;xlabel="particle radius")

```

I get an error:

```julia
ERROR: DimensionError: and μm are not dimensionally compatible.

```

Sure, I can redefine my function for a unitless input, but is there a more elegant solution? What is happening here?

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [September 11, 2023, 8:38am UTC](https://discourse.julialang.org/t/annotated-unitful-axis-in-plots/103732/2 "2023-09-11T08:38:10Z")

</div>

Use `xlabel!`. ~~Your approach adds another subplot which is probably not what you want.~~ This is not true, at least on a current version (1.39.0) and both command work the same as noted by @rafael.guerra below.

@Eben60 Which version of Plots are you running?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 11, 2023, 10:13am UTC](https://discourse.julialang.org/t/annotated-unitful-axis-in-plots/103732/3 "2023-09-11T10:13:20Z")

</div>

For me, `plot!(;xlabel="x")` is the same as `xlabel!("x")"`, both work without error. However, in both cases the x-axis units are lost and not appended to the label.

_PS:_  
_Unitful v1.17.0, Plots v1.39.0_

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [September 11, 2023, 11:28am UTC](https://discourse.julialang.org/t/annotated-unitful-axis-in-plots/103732/4 "2023-09-11T11:28:02Z")

</div>

This works, x-axis labeled as ` particle radius (µm)`

```julia
p=nothing
firstrun = true
for freq in (2:2:14) .* u"MHz"
    global firstrun, p
    prel(r) = Prel(r, freq, σ)
    xlim = (5u"µm", 100u"µm")
    if firstrun
        plot(prel; xlim, label="$freq", xlabel="particle radius")
    else
        p= plot!(prel; xlim, label="$freq")
    end
    firstrun = false
end
p = plot!(;ylabel="a.u.")
display(p)

```

and this errors:

```julia
p=nothing
firstrun = true
for freq in (2:2:14) .* u"MHz"
    global firstrun, p
    prel(r) = Prel(r, freq, σ)
    xlim = (5u"µm", 100u"µm")
    if firstrun
        plot(prel; xlim, label="$freq")
    else
        p= plot!(prel; xlim, label="$freq")
    end
    firstrun = false
end
p = plot!(;ylabel="a.u.", xlabel="particle radius")
display(p)

```

and this errors either:

```julia
xlabel!("particle radius")
p = plot!(;ylabel="a.u.")
display(p)

```

_Unitful v1.17.0, Plots v1.39.0 (updated from 1.15 and 1.38)_

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [September 12, 2023, 8:44am UTC](https://discourse.julialang.org/t/annotated-unitful-axis-in-plots/103732/5 "2023-09-12T08:44:11Z")

</div>

I have filed an issue with Plots.

> <https://github.com/JuliaPlots/Plots.jl/issues/4822>
>
> \# MWE
> 
> \`\`\`julia
> using Plots, Unitful
> 
> julia\> y(t) = 1/2\*t^2;
> 
> julia\> plot…(y, xlims=(0u"s", 5u"s"))
> 
> julia\> xlabel!("Time")
> Error showing value of type Plots.Plot{Plots.GRBackend}:
> ERROR: DimensionError: and s are not dimensionally compatible.
> \`\`\`
> 
> \<details\>\<summary\>Full stacktrace\</summary\>
> 
> \`\`\`julia
> Error showing value of type Plots.Plot{Plots.GRBackend}:
> ERROR: DimensionError: and s are not dimensionally compatible.
> Stacktrace:
> \[1\] #s103#141
> @ ~/.julia/packages/Unitful/J4AJj/src/conversion.jl:7 \[inlined\]
> \[2\] var"#s103#141"(::Any, s::Any, t::Any)
> @ Unitful ./none:0
> \[3\] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any})
> @ Core ./boot.jl:602
> \[4\] uconvert(a::Unitful.FreeUnits{(), NoDims, nothing}, x::Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}})
> @ Unitful ~/.julia/packages/Unitful/J4AJj/src/conversion.jl:72
> \[5\] ustrip
> @ ~/.julia/packages/Unitful/J4AJj/src/utils.jl:35 \[inlined\]
> \[6\] \_ustrip(u::Unitful.FreeUnits{(), NoDims, nothing}, x::Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}})
> @ UnitfulExt ~/.julia/packages/Plots/sxUvK/ext/UnitfulExt.jl:333
> \[7\] \_broadcast\_getindex\_evalf
> @ ./broadcast.jl:683 \[inlined\]
> \[8\] \_broadcast\_getindex
> @ ./broadcast.jl:656 \[inlined\]
> \[9\] #31
> @ ./broadcast.jl:1088 \[inlined\]
> \[10\] ntuple
> @ ./ntuple.jl:49 \[inlined\]
> \[11\] copy(bc::Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(UnitfulExt.\_ustrip), Tuple{Base.RefValue{Unitful.FreeUnits{(), NoDims, nothing}}, Tuple{Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}}}})
> @ Base.Broadcast ./broadcast.jl:1088
> \[12\] materialize
> @ ./broadcast.jl:873 \[inlined\]
> \[13\] process\_limits(lims::Tuple{Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}, Quantity{Int64, 𝐓, Unitful.FreeUnits{(s,), 𝐓, nothing}}}, axis::Plots.Axis)
> @ UnitfulExt ~/.julia/packages/Plots/sxUvK/ext/UnitfulExt.jl:328
> \[14\] widen\_factor(axis::Plots.Axis; factor::Float64)
> @ Plots ~/.julia/packages/Plots/sxUvK/src/axes.jl:618
> \[15\] widen\_factor
> @ ~/.julia/packages/Plots/sxUvK/src/axes.jl:608 \[inlined\]
> \[16\] axis\_limits(sp::Plots.Subplot{Plots.GRBackend}, letter::Symbol)
> @ Plots ~/.julia/packages/Plots/sxUvK/src/axes.jl:654
> \[17\] get\_ticks(sp::Plots.Subplot{Plots.GRBackend}, axis::Plots.Axis; update::Bool, formatter::Symbol)
> @ Plots ~/.julia/packages/Plots/sxUvK/src/axes.jl:229
> \[18\] get\_ticks(sp::Plots.Subplot{Plots.GRBackend}, axis::Plots.Axis)
> @ Plots ~/.julia/packages/Plots/sxUvK/src/axes.jl:214
> \[19\] \_broadcast\_getindex\_evalf
> @ ./broadcast.jl:683 \[inlined\]
> \[20\] \_broadcast\_getindex
> @ ./broadcast.jl:656 \[inlined\]
> \[21\] #31
> @ ./broadcast.jl:1088 \[inlined\]
> \[22\] ntuple
> @ ./ntuple.jl:50 \[inlined\]
> \[23\] copy(bc::Base.Broadcast.Broadcasted{Base.Broadcast.Style{Tuple}, Nothing, typeof(Plots.get\_ticks), Tuple{Base.RefValue{Plots.Subplot{Plots.GRBackend}}, Tuple{Plots.Axis, Plots.Axis, Plots.Axis}}})
> @ Base.Broadcast ./broadcast.jl:1088
> \[24\] materialize
> @ ./broadcast.jl:873 \[inlined\]
> \[25\] \_update\_min\_padding!(sp::Plots.Subplot{Plots.GRBackend})
> @ Plots ~/.julia/packages/Plots/sxUvK/src/backends/gr.jl:812
> \[26\] iterate
> @ ./generator.jl:47 \[inlined\]
> \[27\] \_collect(c::Matrix{AbstractLayout}, itr::Base.Generator{Matrix{AbstractLayout}, typeof(Plots.\_update\_min\_padding!)}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{2})
> @ Base ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/base/array.jl:802
> \[28\] collect\_similar
> @ ./array.jl:711 \[inlined\]
> \[29\] map
> @ ./abstractarray.jl:3263 \[inlined\]
> \[30\] \_update\_min\_padding!(layout::Plots.GridLayout)
> @ Plots ~/.julia/packages/Plots/sxUvK/src/layouts.jl:277
> \[31\] prepare\_output(plt::Plots.Plot{Plots.GRBackend})
> @ Plots ~/.julia/packages/Plots/sxUvK/src/plot.jl:239
> \[32\] display(#unused#::Plots.PlotsDisplay, plt::Plots.Plot{Plots.GRBackend})
> @ Plots ~/.julia/packages/Plots/sxUvK/src/output.jl:168
> \[33\] display(x::Any)
> @ Base.Multimedia ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/base/multimedia.jl:340
> \[34\] print\_response(errio::IO, response::Any, show\_value::Bool, have\_color::Bool, specialdisplay::Union{Nothing, AbstractDisplay})
> @ REPL ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/REPL.jl:0
> \[35\] (::REPL.var"#57#58"{REPL.LineEditREPL, Pair{Any, Bool}, Bool, Bool})(io::Any)
> @ REPL ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/REPL.jl:287
> \[36\] with\_repl\_linfo(f::Any, repl::REPL.LineEditREPL)
> @ REPL ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/REPL.jl:557
> \[37\] print\_response(repl::REPL.AbstractREPL, response::Any, show\_value::Bool, have\_color::Bool)
> @ REPL ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/REPL.jl:285
> \[38\] (::REPL.var"#do\_respond#80"{Bool, Bool, REPL.var"#93#103"{REPL.LineEditREPL, REPL.REPLHistoryProvider}, REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::Any, ok::Bool)
> @ REPL ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/REPL.jl:899
> \[39\] #invokelatest#2
> @ ./essentials.jl:819 \[inlined\]
> \[40\] invokelatest
> @ ./essentials.jl:816 \[inlined\]
> \[41\] run\_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState)
> @ REPL.LineEdit ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/LineEdit.jl:2647
> \[42\] run\_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef)
> @ REPL ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/REPL.jl:1300
> \[43\] (::REPL.var"#62#68"{REPL.LineEditREPL, REPL.REPLBackendRef})()
> @ REPL ./task.jl:514
> \`\`\`
> 
> \</details\> 
> 
> \## Details
> 
> \`process\_limits\` tries to convert the unitful limits to the unit of the axis, which is extracted from the label and in this case does not carry a unit. This appears logically unsound.
> https://github.com/JuliaPlots/Plots.jl/blob/7cd27c470e10d8364ab935935eb58a97eae55ce3/ext/UnitfulExt.jl#L328-L329
> 
> 
> \### Backends
> 
> This bug occurs on ( insert \`x\` below )
> 
> Backend | yes | no | untested
> \-------------|-----|-----|---------
> gr (default) | x | |
> pythonplot | | |x
> plotlyjs | | |x
> pgfplotsx | | |x
> unicodeplots | | |x
> inspectdr | | |x
> gaston | | |x
> 
> \### Versions
> 
> Plots.jl version: 1.39.0
> Backend version (\`\]st -m \<backend(s)\>\`): GR@0.72.9
> Output of \`versioninfo()\`:
> \<details\>\<summary\>versioninfo\</summary\>
> 
> \`\`\`
> Julia Version 1.9.3
> Commit bed2cd540a1 (2023-08-24 14:43 UTC)
> Build Info:
> Official https://julialang.org/ release
> Platform Info:
> OS: macOS (arm64-apple-darwin22.4.0)
> CPU: 8 × Apple M1
> WORD\_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
> Threads: 4 on 4 virtual cores
> Environment:
> JULIA\_NUM\_THREADS = 4
> JULIA\_EDITOR = code
> JULIA\_CONDAPKG\_BACKEND = Current
> JULIA\_IMAGE\_THREADS = 1
> DYLD\_FALLBACK\_LIBRARY\_PATH = /Users/skleinbo/.julia/artifacts/2a86ef020f132332b2f4be2fb40912cc7df5da29/lib:/Users/skleinbo/.julia/artifacts/92b949e2f3a66439c69a8d334fc95810fbd9df9b/lib:/Users/skleinbo/.julia/artifacts/5b338c8fa90c05e6faea86e54d2996cca76cfbbe/lib:/Users/skleinbo/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/lib/julia:/Users/skleinbo/.julia/artifacts/c7f903a3738cc550fb96a33e58e19560477152cb/lib:/Users/skleinbo/.julia/artifacts/9410bad2635eda2239b4a72ba4316c4aa8f5b76e/lib:/Users/skleinbo/.julia/artifacts/95f332911a8dd5ab82620ba58c217193bd0d515d/lib:/Users/skleinbo/.julia/artifacts/09f03e36eda588bb3a9ba375a1987f65e31538db/lib:/Users/skleinbo/.julia/artifacts/9a76a401f82e0e3cafce618fb8d2d5c307ab2836/lib:/Users/skleinbo/.julia/artifacts/b917751a0a1532e56881e471e0f9b441460f2295/lib:/Users/skleinbo/.julia/artifacts/c65e07e3da4f1bf519bc432389dbbd61df320457/lib:/Users/skleinbo/.julia/artifacts/4ec62d729213a748d2300dd0832ebe8ed2292093/lib:/Users/skleinbo/.julia/artifacts/e6b9fb44029423f5cd69e0cbbff25abcc4b32a8f/lib:/Users/skleinbo/.julia/artifacts/b2108f561a8812e376eb80e71a24a3678a24d231/lib:/Users/skleinbo/.julia/artifacts/ada2a202928dd4cb2fc4bd18c4efa9d5455ec742/lib:/Users/skleinbo/.julia/artifacts/df3881e810714d6a09467fe85a6fde79385fe702/lib:/Users/skleinbo/.julia/artifacts/3b3d0bcaf14a9b239a4f4dc20ef7b9e63030a47e/lib:/Users/skleinbo/.julia/artifacts/abf161ac3d4df76ae74bbf5432b7e061b3876236/lib:/Users/skleinbo/.julia/artifacts/4260cf51a368d8e305a5de3669e32539e1e6cc72/lib:/Users/skleinbo/.julia/artifacts/fc7ba632b72ce7d852c1924aa2bbfe244a71c780/lib:/Users/skleinbo/.julia/artifacts/413111420faa4e2aeaa383c075eaa213402d939c/lib:/Users/skleinbo/.julia/artifacts/ca2831bf6edc5088aec5b329ea98364951d6cad0/lib:/Users/skleinbo/.julia/artifacts/3fe6bf926e57cc4be598151cd40832221de2e894/lib:/Users/skleinbo/.julia/artifacts/c325a23bc1f6521474cef5f634f18c8ab311bb02/lib:/Users/skleinbo/.julia/artifacts/0db9c3f6cf936a0da49e2ba954ba3e10bed6ad72/lib:/Users/skleinbo/.julia/artifacts/1a7e22e66b523d9cb884cf85c3ec065b5fb3e5c3/lib:/Users/skleinbo/.julia/artifacts/8a75e6f9180ffb983d34538b242dcb1f3576bbc9/lib:/Users/skleinbo/.julia/artifacts/4609432e7098d8434a7a4c7876dd5b9e09b2a5e7/lib:/Users/skleinbo/.julia/artifacts/bf37190b92ac2fc3dd5e7073ff7ec7bbfd10343f/lib:/Users/skleinbo/.julia/artifacts/7f4d1479db8bfb628aff3806c483e5fec617271a/lib:/Users/skleinbo/.julia/artifacts/9472204d25ab69d52d571b650fdc9d562455ca4a/lib:/Users/skleinbo/.julia/artifacts/b450526929615030746974fd622effa333c2c87a/lib:/Users/skleinbo/.julia/artifacts/5d7ac54565aa955be0d3ff53c9ccebff5422f807/lib:/Users/skleinbo/.julia/artifacts/9a9f59eab237f7454fee1d6ab112a254032540b7/lib:/Users/skleinbo/.julia/artifacts/2107e7bc404f11b178cb9724cb371ef704995727/lib:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtConcurrent.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtCore.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtDBus.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtGui.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtNetwork.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtOpenGL.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtPrintSupport.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtSql.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtTest.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtWidgets.framework/Versions/A:/Users/skleinbo/.julia/artifacts/ed8b0e21b28aaf4ed991d176af731a3194ed83c6/lib/QtXml.framework/Versions/A:/Users/skleinbo/.julia/artifacts/808dd293d07b5fd6bdb12e6f00295ae83fd3c32c/lib:/Users/skleinbo/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/bin/../lib/julia:/Users/skleinbo/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/bin/../lib:
> \`\`\`
> 
> \</details\>
