Skip to content

Commit b3e5ac3

Browse files
committed
added limited support for logarithmic units
1 parent 7741be7 commit b3e5ac3

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

src/UnitfulRecipes.jl

+35-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module UnitfulRecipes
22

33
using RecipesBase
4-
using Unitful: Quantity, unit, ustrip, Unitful, dimension, Units
4+
using Unitful: Quantity, unit, ustrip, Unitful, dimension, Units, LogScaled, logunit, MixedUnits, Level, Gain
55
export @P_str
66

77
const clims_types = (:contour, :contourf, :heatmap, :surface)
@@ -10,11 +10,12 @@ const clims_types = (:contour, :contourf, :heatmap, :surface)
1010
Main recipe
1111
==========#
1212

13-
@recipe function f(::Type{T}, x::T) where T <: AbstractArray{<:Union{Missing,<:Quantity}}
13+
@recipe function f(::Type{T}, x::T) where T <: AbstractArray{<:Union{Missing,<:Quantity, <:LogScaled}}
1414
axisletter = plotattributes[:letter] # x, y, or z
1515
if (axisletter == :z) &&
1616
get(plotattributes, :seriestype, :nothing) clims_types
17-
u = get(plotattributes, :zunit, unit(eltype(x)))
17+
# u = get(plotattributes, :zunit, unit(eltype(x)))
18+
u = get(plotattributes, :zunit, eltype(x) <: LogScaled ? logunit(eltype(x)) : unit(eltype(x)))
1819
ustripattribute!(plotattributes, :clims, u)
1920
append_unit_if_needed!(plotattributes, :colorbar_title, u)
2021
end
@@ -30,7 +31,7 @@ function fixaxis!(attr, x, axisletter)
3031
axisunit = Symbol(axisletter, :unit) # xunit, yunit, zunit
3132
axis = Symbol(axisletter, :axis) # xaxis, yaxis, zaxis
3233
# Get the unit
33-
u = pop!(attr, axisunit, unit(eltype(x)))
34+
u = pop!(attr, axisunit, eltype(x) <: LogScaled ? logunit(eltype(x)) : unit(eltype(x)))
3435
# If the subplot already exists with data, get its unit
3536
sp = get(attr, :subplot, 1)
3637
if sp length(attr[:plot_object]) && attr[:plot_object].n > 0
@@ -54,22 +55,28 @@ function fixaxis!(attr, x, axisletter)
5455
fixmarkersize!(attr)
5556
fixlinecolor!(attr)
5657
# Strip the unit
57-
ustrip.(u, x)
58+
if eltype(x) <: Level
59+
ustrip(x)
60+
elseif eltype(x) <: Gain
61+
getfield.(x, :val)
62+
else
63+
ustrip.(u, x)
64+
end
5865
end
5966

6067
# Recipe for (x::AVec, y::AVec, z::Surface) types
6168
const AVec = AbstractVector
6269
const AMat{T} = AbstractArray{T,2} where T
6370
@recipe function f(x::AVec, y::AVec, z::AMat{T}) where T <: Quantity
64-
u = get(plotattributes, :zunit, unit(eltype(z)))
71+
u = get(plotattributes, :zunit, eltype(z) <: LogScaled ? logunit(eltype(z)) : unit(eltype(z)))
6572
ustripattribute!(plotattributes, :clims, u)
6673
z = fixaxis!(plotattributes, z, :z)
6774
append_unit_if_needed!(plotattributes, :colorbar_title, u)
6875
x, y, z
6976
end
7077

7178
# Recipe for vectors of vectors
72-
@recipe function f(::Type{T}, x::T) where T <: AbstractVector{<:AbstractVector{<:Union{Missing,<:Quantity}}}
79+
@recipe function f(::Type{T}, x::T) where T <: AbstractVector{<:AbstractVector{<:Union{Missing,<:Quantity, <:LogScaled}}}
7380
axisletter = plotattributes[:letter] # x, y, or z
7481
[fixaxis!(plotattributes, x, axisletter) for x in x]
7582
end
@@ -81,19 +88,19 @@ end
8188
end
8289

8390
# Recipes for functions
84-
@recipe function f(f::Function, x::T) where T <: AVec{<:Union{Missing,<:Quantity}}
91+
@recipe function f(f::Function, x::T) where T <: AVec{<:Union{Missing,<:Quantity, <:LogScaled}}
8592
x, f.(x)
8693
end
87-
@recipe function f(x::T, f::Function) where T <: AVec{<:Union{Missing,<:Quantity}}
94+
@recipe function f(x::T, f::Function) where T <: AVec{<:Union{Missing,<:Quantity, <:LogScaled}}
8895
x, f.(x)
8996
end
90-
@recipe function f(x::T, y::AVec, f::Function) where T <: AVec{<:Union{Missing,<:Quantity}}
97+
@recipe function f(x::T, y::AVec, f::Function) where T <: AVec{<:Union{Missing,<:Quantity, <:LogScaled}}
9198
x, y, f.(x',y)
9299
end
93-
@recipe function f(x::AVec, y::T, f::Function) where T <: AVec{<:Union{Missing,<:Quantity}}
100+
@recipe function f(x::AVec, y::T, f::Function) where T <: AVec{<:Union{Missing,<:Quantity, <:LogScaled}}
94101
x, y, f.(x',y)
95102
end
96-
@recipe function f(x::T1, y::T2, f::Function) where {T1<:AVec{<:Union{Missing,<:Quantity}}, T2<:AVec{<:Union{Missing,<:Quantity}}}
103+
@recipe function f(x::T1, y::T2, f::Function) where {T1<:AVec{<:Union{Missing,<:Quantity, <:LogScaled}}, T2<:AVec{<:Union{Missing,<:Quantity, <:LogScaled}}}
97104
x, y, f.(x',y)
98105
end
99106
@recipe function f(f::Function, u::Units)
@@ -138,8 +145,16 @@ fixlinecolor!(attr) = ustripattribute!(attr, :line_z)
138145
function ustripattribute!(attr, key)
139146
if haskey(attr, key)
140147
v = attr[key]
141-
u = unit(eltype(v))
142-
attr[key] = ustrip.(u, v)
148+
if eltype(v) <: Level
149+
u = logunit(eltype(v))
150+
attr[key] = ustrip(v)
151+
elseif eltype(v) <: Gain
152+
u = logunit(eltype(v))
153+
attr[key] = v.val
154+
else
155+
u = unit(eltype(v))
156+
attr[key] = ustrip.(u, v)
157+
end
143158
return u
144159
else
145160
return Unitful.NoUnits
@@ -149,7 +164,11 @@ end
149164
function ustripattribute!(attr, key, u)
150165
if haskey(attr, key)
151166
v = attr[key]
152-
if eltype(v) <: Quantity
167+
if eltype(v) <: Level
168+
attr[key] = ustrip(v)
169+
elseif eltype(v) <: Gain
170+
attr[key] = v.val
171+
elseif eltype(v) <: Quantity
153172
attr[key] = ustrip.(u, v)
154173
end
155174
end
@@ -198,7 +217,7 @@ end
198217
Append unit to labels when appropriate
199218
=====================================#
200219

201-
function append_unit_if_needed!(attr, key, u::Unitful.Units)
220+
function append_unit_if_needed!(attr, key, u::Union{Unitful.Units, Unitful.MixedUnits})
202221
label = get(attr, key, nothing)
203222
append_unit_if_needed!(attr, key, label, u)
204223
end

test/runtests.jl

+22
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,25 @@ end
299299
plot!(plt, (1:3)m)
300300
@test yguide(plt) == "m"
301301
end
302+
303+
@testset "LogScaled plots" begin
304+
x, y = randn(3)*u"dBm", randn(3)*u"dB"
305+
306+
@testset "no keyword argument" begin
307+
@test xguide(plot(x,y)) == "dBm"
308+
@test xseries(plot(x,y)) ustrip.(x)
309+
@test yguide(plot(x,y)) == "dB"
310+
@test yseries(plot(x,y)) ustrip.(y)
311+
end
312+
313+
@testset "labels" begin
314+
@test xguide(plot(x, y, xlabel= "hello")) == "hello (dBm)"
315+
@test xguide(plot(x, y, xlabel=P"hello")) == "hello"
316+
@test yguide(plot(x, y, ylabel= "hello")) == "hello (dB)"
317+
@test yguide(plot(x, y, ylabel=P"hello")) == "hello"
318+
@test xguide(plot(x, y, xlabel= "hello", ylabel= "hello")) == "hello (dBm)"
319+
@test xguide(plot(x, y, xlabel=P"hello", ylabel=P"hello")) == "hello"
320+
@test yguide(plot(x, y, xlabel= "hello", ylabel= "hello")) == "hello (dB)"
321+
@test yguide(plot(x, y, xlabel=P"hello", ylabel=P"hello")) == "hello"
322+
end
323+
end

0 commit comments

Comments
 (0)