# ANN: UnitfulRecipes.jl

**URL:** <https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280>\
**Category:** Package Announcements\
**Tags:** package, announcement, plotting, unitful\
**Created:** [September 2, 2019, 7:31am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280 "2019-09-02T07:31:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 2, 2019, 7:31am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/1 "2019-09-02T07:31:39Z")

</div>

[UnitfulRecipes.jl](https://github.com/jw3126/UnitfulRecipes.jl) is a tiny package, that makes plotting Unitful quantities more convenient:

```julia
using Unitful: cm, kg
using UnitfulRecipes
using Plots

xs = 1cm:1cm:10cm
ys = randn(10)*kg
plot(xs, ys)

```

![img](https://global.discourse-cdn.com/julialang/original/3X/d/e/de799ba55874bab1598ca49d47d21254240d46a6.png)  
It was inspired by the unregistered [UnitfulPlots](https://github.com/PainterQubits/UnitfulPlots.jl).

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 2, 2019, 9:00am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/2 "2019-09-02T09:00:01Z")

</div>

Does it work with unitful DiffEq outputs? that was the biggest problem with UnitfulPlots.jl

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 2, 2019, 9:46am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/3 "2019-09-02T09:46:41Z")

</div>

No idea. Can you give a MWE?

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [September 2, 2019, 9:57am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/4 "2019-09-02T09:57:41Z")

</div>

Sorry I’m not set up to do that right now, it was a while ago and the examples I do have are definately maximal. I remember discussing with @ChrisRackauckas somewhere but neither of us succeeded getting it working.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [September 2, 2019, 12:14pm UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/5 "2019-09-02T12:14:40Z")

</div>

Hmmmm… should this package be part of RecipesBase.jl or Plots.jl itself? We shouldn’t ask users to load a package just to plot with units IMO.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 2, 2019, 7:35pm UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/6 "2019-09-02T19:35:01Z")

</div>

Ok, here an example which semi-works:

```julia
using OrdinaryDiffEq
using Unitful: m, s
using Plots, UnitfulRecipes

const g = 9.81m/s^2
function traj2(u, p, t)
    x,y,vx,vy = u
    [vx,
    vy,
    0.0 * g, # make dimensionally consistent
    -g]
end

tspan = (0.0s, 30.0s)
u₀ = [0.0m, 0.0m, 100m/s, 100m/s]
sol = solve(ODEProblem(traj2, u₀, tspan), Tsit5())
plot(sol[1,:],sol[2,:]) # works
plot(sol) # does not work
#-> ERROR: DimensionError: m and m s^-1 are not dimensionally compatible.                                                                                                    

```

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 2, 2019, 7:45pm UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/7 "2019-09-02T19:45:17Z")

</div>

I don’t think its a good idea for either `Plots.jl` or `RecipesBase` to depend on `Unitful`. I think the only reasonable candidate to include `UnitfulRecipes` would be `Unitful.jl`.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 2, 2019, 8:00pm UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/8 "2019-09-02T20:00:00Z")

</div>

Where is the recipe for `sol` defined? Is the issue that it hands a matrix with mixed dimensions (`m` and `m/s`) to Plots?  
Edit:

```julia
RecipesBase.apply_recipe(Dict{Symbol, Any}(),sol )[1].args[2]

```

shows that it indeed hands a matrix with mixed units over to plots. So producing a unitful plot from this is tricky.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 3, 2019, 4:42am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/9 "2019-09-03T04:42:08Z")

</div>

> [@jw3126](#):
>
> Where is the recipe for `sol` defined?

I think it’s here: [https://github.com/JuliaDiffEq/DiffEqBase.jl/blob/master/src/solutions/solution\_interface.jl](https://github.com/JuliaDiffEq/DiffEqBase.jl/blob/master/src/solutions/solution_interface.jl)

Maybe the simplest thing could be to do something equivalent to `vars = (1, 2, ...)` when the units are different? [http://docs.juliadiffeq.org/latest/basics/plot.html#Choosing-Variables-1](http://docs.juliadiffeq.org/latest/basics/plot.html#Choosing-Variables-1)

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [September 3, 2019, 6:07am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/10 "2019-09-03T06:07:06Z")

</div>

This works:

```julia
julia> plot(sol, vars=[(0,1), (0,2)])

```

i.e. only plotting columns which have the same units.

The most elegant would be if the units were moved to the legend and the axis would not have any units. But that’s maybe more than @jw3126 signed up for.

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [September 3, 2019, 6:51am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/11 "2019-09-03T06:51:05Z")

</div>

Thanks for registering this, it’s just what I needed. I’ve been using `UnitfulPlots` for some debug plotting and loading it manually but it’s much better to have a registered package for this.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 3, 2019, 7:22am UTC](https://discourse.julialang.org/t/ann-unitfulrecipes-jl/28280/12 "2019-09-03T07:22:26Z")

</div>

At the moment I don’t need mixed units myself, so chances are low that I will implement it. PRs are appreciated though.
