1
1
module UnitfulRecipes
2
2
3
3
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
5
5
export @P_str
6
6
7
7
const clims_types = (:contour , :contourf , :heatmap , :surface )
@@ -10,11 +10,12 @@ const clims_types = (:contour, :contourf, :heatmap, :surface)
10
10
Main recipe
11
11
==========#
12
12
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 }}
14
14
axisletter = plotattributes[:letter ] # x, y, or z
15
15
if (axisletter == :z ) &&
16
16
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)))
18
19
ustripattribute! (plotattributes, :clims , u)
19
20
append_unit_if_needed! (plotattributes, :colorbar_title , u)
20
21
end
@@ -30,7 +31,7 @@ function fixaxis!(attr, x, axisletter)
30
31
axisunit = Symbol (axisletter, :unit ) # xunit, yunit, zunit
31
32
axis = Symbol (axisletter, :axis ) # xaxis, yaxis, zaxis
32
33
# 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)))
34
35
# If the subplot already exists with data, get its unit
35
36
sp = get (attr, :subplot , 1 )
36
37
if sp ≤ length (attr[:plot_object ]) && attr[:plot_object ]. n > 0
@@ -54,22 +55,28 @@ function fixaxis!(attr, x, axisletter)
54
55
fixmarkersize! (attr)
55
56
fixlinecolor! (attr)
56
57
# 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
58
65
end
59
66
60
67
# Recipe for (x::AVec, y::AVec, z::Surface) types
61
68
const AVec = AbstractVector
62
69
const AMat{T} = AbstractArray{T,2 } where T
63
70
@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)))
65
72
ustripattribute! (plotattributes, :clims , u)
66
73
z = fixaxis! (plotattributes, z, :z )
67
74
append_unit_if_needed! (plotattributes, :colorbar_title , u)
68
75
x, y, z
69
76
end
70
77
71
78
# 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 }}}
73
80
axisletter = plotattributes[:letter ] # x, y, or z
74
81
[fixaxis! (plotattributes, x, axisletter) for x in x]
75
82
end
81
88
end
82
89
83
90
# 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 }}
85
92
x, f .(x)
86
93
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 }}
88
95
x, f .(x)
89
96
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 }}
91
98
x, y, f .(x' ,y)
92
99
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 }}
94
101
x, y, f .(x' ,y)
95
102
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 }} }
97
104
x, y, f .(x' ,y)
98
105
end
99
106
@recipe function f (f:: Function , u:: Units )
@@ -138,8 +145,16 @@ fixlinecolor!(attr) = ustripattribute!(attr, :line_z)
138
145
function ustripattribute! (attr, key)
139
146
if haskey (attr, key)
140
147
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
143
158
return u
144
159
else
145
160
return Unitful. NoUnits
149
164
function ustripattribute! (attr, key, u)
150
165
if haskey (attr, key)
151
166
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
153
172
attr[key] = ustrip .(u, v)
154
173
end
155
174
end
198
217
Append unit to labels when appropriate
199
218
=====================================#
200
219
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} )
202
221
label = get (attr, key, nothing )
203
222
append_unit_if_needed! (attr, key, label, u)
204
223
end
0 commit comments