jw3126 / UnitfulRecipes.jl

Plots.jl recipes for Unitful.jl arrays
MIT License
37 stars 10 forks source link

Add recipes for Unitful xerror/yerror/zerror #43

Closed briochemc closed 3 years ago

briochemc commented 3 years ago

Fixes #42

Output of this MWE from #42

using Unitful, Plots, UnitfulRecipes
momenta = [p*1u"GeV/c" for p in 1.0:0.1:10]
masses = [139.57039u"MeV/c^2"]
piMass = ones(length(momenta))*masses[1]
piErr=[10.882833439073304u"MeV/c^2" for p in momenta]
yerr=piErr
plot(momenta, [piMass], yerr=yerr, label=["pion"])

with this PR (tested on Julia 1.6)

Screen Shot 2021-03-02 at 10 34 09 am
briochemc commented 3 years ago

FWIW, in this PR I essentially modified fixlims! a bit to make it more general and work for vectors. I also renamed it as ustripattribute!, which already existed to strip units of an attribute, except this one has 3 arguments, where the 3rd argument is the prescribed unit to convert to (when stripping the unit of the attribute). For the reminder, this was done so that if a user plots an x in units of s (seconds) and adds an xerror or xlims in ms, then the correct conversion (to seconds) is applied.

Overall this actually seems cleaner to me (more Julian — yay multiple dispatch) and more generalizable if someone else comes up with another attribute to add (e.g., ribbons?) 😄