Skip to content

Commit

Permalink
Merge pull request #50 from gustaphe/function
Browse files Browse the repository at this point in the history
Plot function against unit
  • Loading branch information
gustaphe committed Apr 6, 2021
2 parents 42a9a37 + cc9330f commit 2e83d84
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 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.1.1"
version = "1.2.0"

[deps]
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Expand Down
13 changes: 13 additions & 0 deletions docs/lit/examples/1_Examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,16 @@ 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="")

# ## Functions
#
# In order to plot a unitful function on a unitful axis, supply as a second argument a
# vector of unitful sample points, or the unit for the independent axis:

model(x) = 1u"V"*exp(-((x-0.5u"s")/0.7u"s")^2)
t = randn(10)u"s" # Sample points
U = model.(t) + randn(10)u"dV" .|> u"V" # Noisy acquicisions
plot(t, U; xlabel="t", ylabel="U", st=:scatter, label="Samples")
plot!(model, t; st=:scatter, label="Noise removed")
plot!(model, u"s"; label="True function")

25 changes: 24 additions & 1 deletion src/UnitfulRecipes.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module UnitfulRecipes

using RecipesBase
using Unitful: Quantity, unit, ustrip, Unitful, dimension
using Unitful: Quantity, unit, ustrip, Unitful, dimension, Units
export @P_str

#==========
Expand Down Expand Up @@ -75,7 +75,30 @@ end
@recipe function f(x::T1, y::T2, f::Function) where {T1<:AVec{<:Union{Missing,<:Quantity}}, T2<:AVec{<:Union{Missing,<:Quantity}}}
x, y, f.(x',y)
end
@recipe function f(f::Function, u::Units)
uf = UnitFunction(f, [u])
recipedata = RecipesBase.apply_recipe(plotattributes, uf)
(_, xmin, xmax) = recipedata[1].args
return f, xmin*u, xmax*u
end

"""
```julia
UnitFunction
```
A function, bundled with the assumed units of each of its inputs.
```julia
f(x, y) = x^2 + y
uf = UnitFunction(f, u"m", u"m^2")
uf(3, 2) == f(3u"m", 2u"m"^2) == 7u"m^2"
```
"""
struct UnitFunction <: Function
f::Function
u::Vector{Units}
end
(f::UnitFunction)(args...) = f.f((args .* f.u)...)

#===============
Attribute fixing
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ end
g(x,y) = x*y*m # If the unit comes from the function only then it throws
@test_throws DimensionError plot(x, y, g) isa Plots.Plot
end
@testset "plot(f, u)" begin
f(x) = x^2
pl = plot(x*m, f.(x*m))
@test plot!(pl, f, m) isa Plots.Plot
@test_throws DimensionError plot!(pl, f, s) isa Plots.Plot
pl = plot(f, m)
@test xguide(pl) == string(m)
@test yguide(pl) == string(m^2)
f(x) = exp(x/(3m))
@test plot(f, u"m") isa Plots.Plot
end
end

@testset "Moar plots" begin
Expand Down

2 comments on commit 2e83d84

@gustaphe
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 register()

@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/33667

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.2.0 -m "<description of version>" 2e83d84b62a876ac0a6b52fa0e2828195cd2dc20
git push origin v1.2.0

Please sign in to comment.