Using Unitful with Plots?

Hi,

Can I plot Unitful numbers with Plots?

using Plots
using Unitful
plot([0.0, 1.0, 2.0]u"s", [3.5, 2.5, 1.5]u"V")

gives

DimensionError: Inf and 0.0 s are not dimensionally compatible.

Is there a way to make it work without stripping out the units before passing it to Plots?

1 Like

there is a package, UnitfulPlots.jl, that maybe is a solution, but i can’t make it work.

2 Likes

In the end, somewhere, you have to strip the units because the backends want floating point numbers. What UnitfulPlots.jl does is supply recipes that check unit compatibility, add labels, and then strip and pass to the next stage of the plotting pipeline.

4 Likes

If you just want a solution to plotting things with Unitful values and don’t care about what package you use to do things, current master of Gadfly should “just work” as is. Unfortunately there hasn’t been a release since the PR adding Unitful support was merged, so you will have to checkout the master branch. Depending on the state of things, you may also have to checkout the master branch of Compose to get Gadfly working properly.

2 Likes

Thanks for the replies - will be back to those in the coming days.