diff --git a/docs/src/examples.md b/docs/src/examples.md index 3dd0633b..8e6dbd4f 100644 --- a/docs/src/examples.md +++ b/docs/src/examples.md @@ -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) diff --git a/docs/src/plot-example-ribbon.png b/docs/src/plot-example-ribbon.png new file mode 100644 index 00000000..efe85ae3 Binary files /dev/null and b/docs/src/plot-example-ribbon.png differ diff --git a/ext/MeasurementsRecipesBaseExt.jl b/ext/MeasurementsRecipesBaseExt.jl index f1449d06..55b9acb2 100644 --- a/ext/MeasurementsRecipesBaseExt.jl +++ b/ext/MeasurementsRecipesBaseExt.jl @@ -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 @@ -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 diff --git a/test/plots.jl b/test/plots.jl index a23a1f26..c42b6df6 100644 --- a/test/plots.jl +++ b/test/plots.jl @@ -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))