Skip to content

Commit

Permalink
fix several Unitful bugs (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg authored Jun 2, 2023
1 parent ad898a0 commit 0aa545b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
16 changes: 9 additions & 7 deletions ext/UnitfulExt.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
2 changes: 2 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
12 changes: 12 additions & 0 deletions test/tst_issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0aa545b

Please sign in to comment.