Skip to content

Commit

Permalink
Add recipes for Unitful xerror/yerror/zerror (#43)
Browse files Browse the repository at this point in the history
* Add recipes for Unitful xerror/yerror/zerror

* Add example of unitful errorbars in docs
  • Loading branch information
briochemc authored Mar 2, 2021
1 parent 4154d53 commit 6f2c40e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UnitfulRecipes"
uuid = "42071c24-d89e-48dd-8a24-8a12d9b8861f"
authors = ["Benoit Pasquier", "Jan Weidner"]
version = "1.0.2"
version = "1.0.3"

[deps]
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Expand Down
10 changes: 10 additions & 0 deletions docs/lit/examples/1_Examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ contour(x, y, z)

contourf(x, y, z)

# ## Error bars

# For example, you can use the `yerror` keyword argument with units,
# which will be converted to the units of `y` and plot your errorbars:

using Unitful: GeV, MeV, c
x = (1.0:0.1:10) * GeV/c
y = @. (2 + sin(x / (GeV/c))) * 0.4GeV/c^2 # a sine to make it pretty
yerror = 10.9MeV/c^2 * exp.(randn(length(x))) # some noise for pretty again
plot(x, y; yerror, title="My unitful data with yerror bars", lab="")

27 changes: 14 additions & 13 deletions src/UnitfulRecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function fixaxis!(attr, x, axisletter)
# Attribute keys
axislabel = Symbol(axisletter, :guide) # xguide, yguide, zguide
axislims = Symbol(axisletter, :lims) # xlims, ylims, zlims
err = Symbol(axisletter, :error) # xerror, yerror, zerror
axisunit = Symbol(axisletter, :unit) # xunit, yunit, zunit
axis = Symbol(axisletter, :axis) # xaxis, yaxis, zaxis
# Get the unit
Expand All @@ -31,7 +32,8 @@ function fixaxis!(attr, x, axisletter)
end
# Fix the attributes: labels, lims, marker/line stuff, etc.
append_unit_if_needed!(attr, axislabel, u)
fixlims!(attr, axislims, u)
ustripattribute!(attr, axislims, u)
ustripattribute!(attr, err, u)
fixmarkercolor!(attr)
fixmarkersize!(attr)
fixlinecolor!(attr)
Expand Down Expand Up @@ -80,22 +82,12 @@ Attribute fixing
# Markers / lines
function fixmarkercolor!(attr)
u = ustripattribute!(attr, :marker_z)
fixlims!(attr, :clims, u)
ustripattribute!(attr, :clims, u)
u == Unitful.NoUnits || append_unit_if_needed!(attr, :colorbar_title, u)
end
fixmarkersize!(attr) = ustripattribute!(attr, :markersize)
fixlinecolor!(attr) = ustripattribute!(attr, :line_z)

# Lims
function fixlims!(attr, key, u)
if haskey(attr, key)
lims = attr[key]
if lims isa NTuple{2, Quantity}
attr[key] = ustrip.(u, lims)
end
end
end

# strip unit from attribute[key]
function ustripattribute!(attr, key)
if haskey(attr, key)
Expand All @@ -107,7 +99,16 @@ function ustripattribute!(attr, key)
return Unitful.NoUnits
end
end

# If supplied, use the unit (optional 3rd argument)
function ustripattribute!(attr, key, u)
if haskey(attr, key)
v = attr[key]
if eltype(v) <: Quantity
attr[key] = ustrip.(u, v)
end
end
u
end

#=======================================
Label string containing unit information
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,14 @@ end
plt = plot(x,y)
@test yguide(plt,1) == "s"
end

@testset "Errors" begin
x = rand(10) * u"mm"
ex = rand(10) * u"μm"
y = rand(10) * u"s"
ey = rand(10) * u"ms"
plt = plot(x, y, xerr=ex, yerr=ey)
@test plt isa Plots.Plot
@test xguide(plt) == "mm"
@test yguide(plt) == "s"
end

2 comments on commit 6f2c40e

@briochemc
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/31121

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.3 -m "<description of version>" 6f2c40e581d51af6a3344276d7e44ca721d917bd
git push origin v1.0.3

Please sign in to comment.