Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncertainty ribbon plot #164

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,15 @@ julia> plot(sin, [x ± 0.1 for x in 1:0.2:10], size = (1200, 800))
```

![image](plot-example.png)

Displaying errors as a ribbon plot is also supported, but only for the y-axis uncertainty (a limitation inherited from `Plots.jl` due to how `ribbon` implements a `fillrange`).

```julia
julia> x = range(0, 6, 301)

julia> y = measurement.(cospi.(x), (5 .+ 2sinpi.(5x))/10)

julia> plot(x, y, uncertainty_plot = :ribbon, color = :red)
```

![image](plot-example-ribbon.png)
Binary file added docs/src/plot-example-ribbon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 26 additions & 5 deletions ext/MeasurementsRecipesBaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@ else
using ..RecipesBase
end

@recipe function f(y::AbstractArray{<:Measurement})
yerror := uncertainty.(y)
unrecognised_uncertainty_plot_message = """
Unrecognized value for `uncertainty_plot` keyword.
Expecting either of `:bar` (default), `:ribbon`, or `:none`.
"""

@recipe function f(y::AbstractArray{<:Measurement}; uncertainty_plot = :bar)
if uncertainty_plot == :ribbon
ribbon := uncertainty.(y)
elseif uncertainty_plot == :bar
yerror := uncertainty.(y)
elseif uncertainty_plot == :none

else
error(unrecognised_uncertainty_plot_message)
end
value.(y)
end

Expand All @@ -47,9 +60,17 @@ end
value.(x), y
end

@recipe function f(x::AbstractArray, y::AbstractArray{<:Measurement})
yerror := uncertainty.(y)
x, value.(y)
@recipe function f(x::AbstractArray, y::AbstractArray{<:Measurement}; uncertainty_plot = :bar)
if uncertainty_plot == :ribbon
ribbon := uncertainty.(y)
elseif uncertainty_plot == :bar
yerror := uncertainty.(y)
elseif uncertainty_plot == :none

else
error(unrecognised_uncertainty_plot_message)
end
x, value.(y)
end

end
8 changes: 8 additions & 0 deletions test/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ rec = RecipesBase.apply_recipe(Dict{Symbol, Any}(), x, value.(y))
rec = RecipesBase.apply_recipe(Dict{Symbol, Any}(), value.(x), y)
@test getfield(rec[1], 1) == Dict{Symbol, Any}(:yerror => uncertainty.(y))
@test rec[1].args == (value.(x), value.(y))

rec = RecipesBase.apply_recipe(Dict{Symbol, Any}(), value.(y); uncertainty_plot = :ribbon)
@test getfield(rec[1], 1) == Dict{Symbol, Any}(:ribbon => uncertainty.(y))
@test rec[1].args == (value.(x), value.(y))

rec = RecipesBase.apply_recipe(Dict{Symbol, Any}(), x, value.(y); uncertainty_plot = :ribbon)
@test getfield(rec[1], 1) == Dict{Symbol, Any}(:ribbon => uncertainty.(y))
@test rec[1].args == (value.(x), value.(y))
Loading