From 0e29d677366d51028d38c201768e96d613241533 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 9 Aug 2024 17:29:26 +0200 Subject: [PATCH] better Makie plotting: consistency and new options Now, point conversion also uses quantiles by default (as rangebars and band did). Added options to request pmean + pstd plotting instead of pquantiles. --- ext/MakieExt.jl | 27 ++++++++++++++++++++++----- test/runtests.jl | 3 +++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ext/MakieExt.jl b/ext/MakieExt.jl index 0665666..f879c58 100644 --- a/ext/MakieExt.jl +++ b/ext/MakieExt.jl @@ -6,11 +6,28 @@ using MonteCarloMeasurements Makie.used_attributes(::Type{<:Series}, ::AbstractVector, ::AbstractVector{<:Particles}) = (:N,) Makie.used_attributes(::Type{<:Series}, ::AbstractVector{<:Tuple{<:Real,<:Particles}}) = (:N,) Makie.convert_arguments(ct::Type{<:Series}, x::AbstractVector, y::AbstractVector{<:Particles}; N=7) = convert_arguments(ct, x, Matrix(y)[1:min(N, end), :]) -Makie.used_attributes(::Type{<:Union{Rangebars,Band}}, ::AbstractVector, ::AbstractVector{<:Particles}) = (:q,) -Makie.used_attributes(::Type{<:Union{Rangebars,Band}}, ::AbstractVector{<:Tuple{<:Real,<:Particles}}) = (:q,) -Makie.convert_arguments(p::Type{<:Union{Hist,Density}}, x::Particles) = convert_arguments(p, Vector(x)) -Makie.convert_arguments(ct::Type{<:Union{Rangebars,Band}}, x::AbstractVector, y::AbstractVector{<:Particles}; q=0.16) = convert_arguments(ct, x, pquantile.(y, q), pquantile.(y, 1-q)) + +Makie.used_attributes(::Type{<:Union{Rangebars,Band}}, ::AbstractVector, ::AbstractVector{<:Particles}) = (:q, :nσ) +Makie.used_attributes(::Type{<:Union{Rangebars,Band}}, ::AbstractVector{<:Tuple{<:Real,<:Particles}}) = (:q, :nσ) +function Makie.convert_arguments(ct::Type{<:Union{Rangebars,Band}}, x::AbstractVector, y::AbstractVector{<:Particles}; q=nothing, nσ=nothing) + !isnothing(q) && !isnothing(nσ) && throw(ArgumentError("Only one of `q`` or `nσ`` can be specified")) + if isnothing(q) && isnothing(nσ) + q = 0.16 + end + if !isnothing(q) + convert_arguments(ct, x, pquantile.(y, q), pquantile.(y, 1-q)) + elseif !isnothing(nσ) + avgs = pmean.(y) + Δs = nσ .* pstd.(y) + convert_arguments(ct, x, avgs .- Δs, avgs .+ Δs) + end +end + +Makie.used_attributes(::PointBased, ::AbstractVector{<:Tuple{<:Real,<:Particles}}) = (:avgfunc,) +Makie.convert_arguments(ct::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Particles}; avgfunc=pmedian) = convert_arguments(ct, x, avgfunc.(y)) + Makie.convert_arguments(ct::Type{<:AbstractPlot}, X::AbstractVector{<:Tuple{<:Real,<:Particles}}; kwargs...) = convert_arguments(ct, first.(X), last.(X); kwargs...) -Makie.convert_arguments(ct::PointBased, x::AbstractVector{<:Real}, y::AbstractVector{<:Particles}) = convert_arguments(ct, x, pmean.(y)) + +Makie.convert_arguments(p::Type{<:Union{Hist,Density}}, x::Particles) = convert_arguments(p, Vector(x)) end diff --git a/test/runtests.jl b/test/runtests.jl index a652aed..d9cc027 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -666,9 +666,12 @@ Random.seed!(0) Makie.scatter(xs, ys) Makie.scatter(tuple.(xs, ys)) + Makie.scatter(tuple.(xs, ys), avgfunc=pmean) Makie.band(xs, ys) Makie.band(tuple.(xs, ys); q=0.01) + Makie.band(tuple.(xs, ys); nσ=2) Makie.rangebars(tuple.(xs, ys); q=0.16) + Makie.rangebars(tuple.(xs, ys); q=0.16, nσ=2) Makie.series(xs, ys) Makie.series(tuple.(xs, ys); N=5) end