From 289e373358eb1c83cfbffca435351ec746a4530c Mon Sep 17 00:00:00 2001 From: gustaphe Date: Fri, 26 May 2023 10:04:26 +0200 Subject: [PATCH] Singleargustrip (#4752) * Test * Remove single arg _ustrips from annotate * Formatting --- ext/UnitfulExt.jl | 27 +++++++++++++++++++++------ test/test_unitful.jl | 3 ++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/ext/UnitfulExt.jl b/ext/UnitfulExt.jl index d9e80ad55..fa983ddfc 100644 --- a/ext/UnitfulExt.jl +++ b/ext/UnitfulExt.jl @@ -287,21 +287,36 @@ getaxisunit(a::Axis) = getaxisunit(a[:guide]) #============== Fix annotations ===============# -Plots.locate_annotation( +function Plots.locate_annotation( sp::Subplot, x::MissingOrQuantity, y::MissingOrQuantity, label::PlotText, -) = (_ustrip(x), _ustrip(y), label) -Plots.locate_annotation( +) + xunit = getaxisunit(sp.attr[:xaxis]) + yunit = getaxisunit(sp.attr[:yaxis]) + (_ustrip(xunit, x), _ustrip(yunit, y), label) +end +function Plots.locate_annotation( sp::Subplot, x::MissingOrQuantity, y::MissingOrQuantity, z::MissingOrQuantity, label::PlotText, -) = (_ustrip(x), _ustrip(y), _ustrip(z), label) -Plots.locate_annotation(sp::Subplot, rel::NTuple{N,<:MissingOrQuantity}, label) where {N} = - Plots.locate_annotation(sp, _ustrip.(rel), label) +) + xunit = getaxisunit(sp.attr[:xaxis]) + yunit = getaxisunit(sp.attr[:yaxis]) + zunit = getaxisunit(sp.attr[:zaxis]) + (_ustrip(xunit, x), _ustrip(yunit, y), _ustrip(zunit, z), label) +end +function Plots.locate_annotation( + sp::Subplot, + rel::NTuple{N,<:MissingOrQuantity}, + label, +) where {N} + units = getaxisunit(sp.attr[:xaxis], sp.attr[:yaxis], sp.attr[:zaxis]) + Plots.locate_annotation(sp, _ustrip.(zip(units, rel)), label) +end #==================# # ticks and limits # diff --git a/test/test_unitful.jl b/test/test_unitful.jl index 3a5a1ac64..efcde6e2d 100644 --- a/test/test_unitful.jl +++ b/test/test_unitful.jl @@ -370,7 +370,8 @@ end @testset "Annotate" begin pl = plot([0, 1]u"s", [0, 1]u"m") annotate!(pl, [0.25]u"s", [0.5]u"m", text("annotation")) - @test show(devnull, pl) isa Nothing + savefig(pl, testfile) + @test length(pl.subplots[1].attr[:annotations]) == 1 end @testset "AbstractProtectedString" begin