From 0aa545bd08158ec03604382f5244288945685a1b Mon Sep 17 00:00:00 2001 From: t-bltg Date: Fri, 2 Jun 2023 17:38:07 +0200 Subject: [PATCH] fix several `Unitful` bugs (#357) --- ext/UnitfulExt.jl | 16 +++++++++------- src/common.jl | 2 ++ src/plot.jl | 2 ++ test/tst_issues.jl | 12 ++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ext/UnitfulExt.jl b/ext/UnitfulExt.jl index 3cf3071b..be5e6b9a 100644 --- a/ext/UnitfulExt.jl +++ b/ext/UnitfulExt.jl @@ -1,6 +1,6 @@ module UnitfulExt -import UnicodePlots: UnicodePlots, KEYWORDS, Plot, Canvas +import UnicodePlots: UnicodePlots, KEYWORDS, Plot, Canvas, unitless UnicodePlots.@ext_imp_use :import Unitful Quantity RealOrRealQuantity ustrip unit function unit_str(x, fancy) @@ -18,6 +18,8 @@ number_unit(x::AbstractVector{<:Quantity}, fancy = true) = ustrip.(x), unit_str(first(x), fancy) number_unit(x::Quantity, fancy = true) = ustrip(x), unit_str(x, fancy) +unitless(x::Quantity) = ustrip(x) # NOTE: keep in sync with src/common.jl + # ---------------------------------------------------------------------------- # # lineplot function UnicodePlots.lineplot( @@ -31,8 +33,8 @@ function UnicodePlots.lineplot( x, ux = number_unit(x, unicode_exponent) y, uy = number_unit(y, unicode_exponent) UnicodePlots.lineplot( - ustrip.(x), - ustrip.(y); + unitless.(x), + unitless.(y); xlabel = unit_label(xlabel, ux), ylabel = unit_label(ylabel, uy), unicode_exponent, @@ -45,7 +47,7 @@ UnicodePlots.lineplot!( x::AbstractVector{<:RealOrRealQuantity}, y::AbstractVector{<:Quantity}; kw..., -) = UnicodePlots.lineplot!(plot, ustrip.(x), ustrip.(y); kw...) +) = UnicodePlots.lineplot!(plot, unitless.(x), unitless.(y); kw...) # ---------------------------------------------------------------------------- # # scatterplot @@ -60,8 +62,8 @@ function UnicodePlots.scatterplot( x, ux = number_unit(x, unicode_exponent) y, uy = number_unit(y, unicode_exponent) UnicodePlots.scatterplot( - ustrip.(x), - ustrip.(y); + unitless.(x), + unitless.(y); xlabel = unit_label(xlabel, ux), ylabel = unit_label(ylabel, uy), unicode_exponent, @@ -74,6 +76,6 @@ UnicodePlots.scatterplot!( x::AbstractVector{<:RealOrRealQuantity}, y::AbstractVector{<:Quantity}; kw..., -) = UnicodePlots.scatterplot!(plot, ustrip.(x), ustrip.(y); kw...) +) = UnicodePlots.scatterplot!(plot, unitless.(x), unitless.(y); kw...) end # module diff --git a/src/common.jl b/src/common.jl index 85b486d7..3bbf21fc 100644 --- a/src/common.jl +++ b/src/common.jl @@ -436,6 +436,8 @@ scale_callback(scale::Function) = scale extend_limits(vec, lims) = extend_limits(vec, lims, :identity) +unitless(x) = x # noop when Unitful is not loaded + function extend_limits(vec, lims, scale::Union{Symbol,Function}) scale = scale_callback(scale) mi, ma = as_float(extrema(lims)) diff --git a/src/plot.jl b/src/plot.jl index 65563b14..57b1c88d 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -205,6 +205,8 @@ function Plot( mvp = create_MVP(projection, x, y, z; kw...) + xlim, ylim = unitless.(xlim), unitless.(ylim) + (mx, Mx), (my, My) = if is_enabled(mvp) (xscale ≢ identity || yscale ≢ identity) && throw(ArgumentError("`xscale` or `yscale` are unsupported in 3D")) diff --git a/test/tst_issues.jl b/test/tst_issues.jl index 49ddf262..18289cfe 100644 --- a/test/tst_issues.jl +++ b/test/tst_issues.jl @@ -83,4 +83,16 @@ p = heatmap(A) test_ref("issues/heatmap_identity.txt", @show_col(p)) end + + @testset "Unitful mix units, lims (#321)" begin + x = 1:3 + y = @. 2x * u"s" + ylim = (0u"s", 8u"s") + + p = lineplot(x, y; ylim) + show(devnull, p) + + p = scatterplot(x, y; ylim) + show(devnull, p) + end end