-
-
Notifications
You must be signed in to change notification settings - Fork 372
Make type recipes aware of current axes. #2503
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
Conversation
|
I think there is an issue if your type is a subtype of Number, then the recipe is ignored. MWE: # copy-paste of issue MWE
struct Measurement1
val::Float64
err::Float64
end
value(m::Measurement1) = m.val
uncertainty(m::Measurement1) = m.err
@recipe function f(::Type{T}, m::T) where T <: AbstractArray{<:Measurement1}
error_sym = Symbol(plotattributes[:letter], :error)
plotattributes[error_sym] = uncertainty.(m)
value.(m)
end
# same but as a subtype of Number
struct Measurement2 <: Number # is the only thing I added here
val::Float64
err::Float64
end
value(m::Measurement2) = m.val
uncertainty(m::Measurement2) = m.err
@recipe function f(::Type{T}, m::T) where T <: AbstractArray{<:Measurement2}
error_sym = Symbol(plotattributes[:letter], :error)
plotattributes[error_sym] = uncertainty.(m)
value.(m)
end
# comparison
ms1 = Measurement1.(10rand(10), rand(10))
ms2 = Measurement2.(10rand(10), rand(10))
scatter(ms1) # works
scatter(ms2) # does not work(FYI, I was trying to apply this new functionality to UnitfulRecipes, but it would not work because |
|
Thanks for reporting! I will see if I can get this to work with subtypes of |
|
@briochemc If I allow custom type recipes for all |
|
That would fix my issue, yes! I don't know about other packages though. Maybe an issue for packages that subtype |
|
If we are applying a type recipe we are doing preprocessing and postprocessing of axis arguments, like setting |
|
I'm not sure I got it but thanks for trying 😅 But was this (skipping for |
Should be fixed in Plots 1.0.1
No, apparently recipes were only skipped for vectors of |
|
Another thing I just noticed is that the type recipe for |
|
Thanks, should be fixed in Plots 1.0.3. It would be great if you could share examples if you have a new type recipe ready, so we can add some tests. |
|
Great! Well I'm working on rewriting UnitfulRecipes in this branch: https://github.com/jw3126/UnitfulRecipes.jl/tree/AxisAware. Will work on it today and see if I can pass all the tests :) |
|
Actually, after checking, 1.0.3 did not fix it for me. Here is a MWE: using Plots, RecipesBase
struct Measurement <: Number
val::Float64
err::Float64
end
value(m::Measurement) = m.val
uncertainty(m::Measurement) = m.err
# now just extract the value and print which axis is being treated
@recipe function f(::Type{T}, m::T) where T <: AbstractArray{<:Measurement}
println(plotattributes[:letter])
value.(m)
end
x = Measurement.(10sort(rand(10)), rand(10))
y = Measurement.(10sort(rand(10)), rand(10))
z = Measurement.(10rand(10,10), rand(10,10))
contour(x, y, z) where you can see that |
|
Additional note/MWE: I could capture it via a recipe like const AVec = AbstractVector
const AMat{T} = AbstractArray{T,2} where T
@recipe function f(x::AVec, y::AVec, z::AMat{T}) where T <: Quantity
println(:z)
x, y, value.(z)
endBut this seems to go against what this PR is all about, right? |
|
Not sure if this makes sense but I would also like to ask. Is it possible to have Maybe this could help for recipe writers? E.g., in the case of Sorry if this is completely missing some point 😅 |
|
Thanks for the example above, I will investigate, where the issue is.
Interesting idea, where would |
|
or you could check in the type recipe for |
|
In both cases there would be an issue if someone defines e.g. a series recipe One way around this would be defining |
|
I stumble on another tiny bug FYI whereby using Plots, RecipesBase, Statistics
struct Measurement <: Number
val::Float64
err::Float64
end
value(m::Measurement) = m.val
uncertainty(m::Measurement) = m.err
# now just extract the value and print which axis is being treated
@recipe function f(::Type{T}, m::T) where T <: AbstractArray{<:Measurement}
println(plotattributes[:letter])
value.(m)
end
x = Measurement.(rand(5), rand(5))
y = Measurement.(rand(5), rand(5))
plot(x, y)
hline!(y)
vline!(x) # the recipe thinks its :letter is :y instead of :xEDIT: opened this as a separate Plots.jl issue |
Temporarily store the current axis letter symbol in
plotattributes, so type recipes can use this information.An example where this could be useful:
Consider a
Measurementtype withvalueanduncertaintyNow we can define a vector type recipe.
This is probably more composible and convenient for package authors than defining user recipes for all combinations of signatures, as it is done in Measurements.jl right now.
scatter(ms1)scatter(ms1, ms2)