@@ -22,7 +22,7 @@ Main recipe
2222 if axisletter === :z && get(plotattributes, :seriestype, :nothing ) ∈ clims_types
2323 u = get(plotattributes, :zunit, _unit(eltype(x)))
2424 ustripattribute!(plotattributes, :clims, u)
25- append_unit_if_needed !(plotattributes, :colorbar_title , u)
25+ append_cbar_unit_if_needed !(plotattributes, u)
2626 end
2727 fixaxis!(plotattributes, x, axisletter)
2828end
@@ -34,7 +34,7 @@ function fixaxis!(attr, x, axisletter)
3434 axis = Symbol(axisletter, :axis) # xaxis, yaxis, zaxis
3535 # if the subplot already exists with data, use that unit
3636 sp = get(attr, :subplot, 1 )
37- if sp ≤ length(attr[:plot_object])
37+ if sp ≤ length(attr[:plot_object]) && attr[:plot_object] . n > 0
3838 spu = getaxisunit(attr[:plot_object][sp][axis])
3939 if ! isnothing(spu)
4040 u = spu
@@ -51,9 +51,9 @@ function fixaxis!(attr, x, axisletter)
5151 ustripattribute!(attr, :fillrange, u)
5252 end
5353 fixaspectratio!(attr, u, axisletter)
54- fixmarkercolor!(attr)
54+ fixseriescolor!(attr, :marker_z)
55+ fixseriescolor!(attr, :line_z)
5556 fixmarkersize!(attr)
56- fixlinecolor!(attr)
5757 _ustrip.(u, x) # strip the unit
5858end
5959
6262 u = get(plotattributes, :zunit, _unit(eltype(z)))
6363 ustripattribute!(plotattributes, :clims, u)
6464 z = fixaxis!(plotattributes, z, :z)
65- append_unit_if_needed !(plotattributes, :colorbar_title , u)
65+ append_cbar_unit_if_needed !(plotattributes, u)
6666 x, y, z
6767end
6868
@@ -154,15 +154,30 @@ function fixaspectratio!(attr, u, axisletter)
154154end
155155
156156# Markers / lines
157- function fixmarkercolor!(attr)
158- u = ustripattribute!(attr, :marker_z)
159- ustripattribute!(attr, :clims, u)
160- u == NoUnits || append_unit_if_needed!(attr, :colorbar_title, u)
161- end
162- function fixlinecolor!(attr)
163- u = ustripattribute!(attr, :line_z)
157+ function fixseriescolor!(attr, key)
158+ sp = get(attr, :subplot, 1)
159+ # Precedence to user-passed zunit
160+ if haskey(attr, :zunit)
161+ u = attr[:zunit]
162+ ustripattribute!(attr, key, u)
163+ # Then to an existing subplot' s colorbar title
164+ elseif sp ≤ length(attr[:plot_object]) && attr[:plot_object]. n > 0
165+ cbar_title = get(attr[:plot_object][sp], :colorbar_title, nothing )
166+ spu = (cbar_title isa UnitfulString ? cbar_title. unit : nothing )
167+ if ! isnothing(spu)
168+ u = spu
169+ ustripattribute!(attr, key, u)
170+ else
171+ u = ustripattribute!(attr, key)
172+ end
173+ # Otherwise, get from the attribute
174+ else
175+ u = ustripattribute!(attr, key)
176+ end
164177 ustripattribute!(attr, :clims, u)
165- u == NoUnits || append_unit_if_needed!(attr, :colorbar_title, u)
178+ # fixseriescolor! is called for each axis, so after the first pass,
179+ # u will be NoUnits and we don't want to append unit again
180+ u == NoUnits || append_cbar_unit_if_needed!(attr, u)
166181end
167182fixmarkersize!(attr) = ustripattribute!(attr, :markersize)
168183
192207
193208#= ======================================
194209Label string containing unit information
210+ Used only for colorbars, etc., which don't
211+ have a bettter place for storing units
195212=======================================#
196213
197214const APS = Plots. AbstractProtectedString
@@ -202,42 +219,45 @@ end
202219
203220#= ====================================
204221Append unit to labels when appropriate
205- This is needed for colorbars, mostly,
206- since axes have their own handling
222+ This is needed for colorbars, etc., since axes have
223+ distinct unit handling
207224=====================================#
208225
209- append_unit_if_needed !(attr, key , u) =
210- append_unit_if_needed !(attr, key, get(attr, key , nothing), u)
226+ append_cbar_unit_if_needed !(attr, u) =
227+ append_cbar_unit_if_needed !(attr, get(attr, :colorbar_title , nothing ), u)
211228# dispatch on the type of `label`
212- append_unit_if_needed!(attr, key, label::Plots.ProtectedString, u) = nothing
213- append_unit_if_needed!(attr, key, label::UnitfulString, u) = nothing
214- function append_unit_if_needed!(attr, key, label::Nothing, u)
215- attr[key] = if attr[:plot_object].backend == Plots.PGFPlotsXBackend()
229+ append_cbar_unit_if_needed!(attr, label:: UnitfulString , u) = nothing
230+ function append_cbar_unit_if_needed!(attr, label:: Nothing , u)
231+ unitformat = get(attr, Symbol(:z, :unitformat), :round)
232+ if unitformat ∈ [:nounit, :none, false , nothing ]
233+ return attr[:colorbar_title] = UnitfulString(" " , u)
234+ end
235+ attr[:colorbar_title] = if Plots. backend_name() === :pgfplotsx
216236 UnitfulString(LaTeXString(latexify(u)), u)
217237 else
218238 UnitfulString(string(u), u)
219239 end
220240end
221- function append_unit_if_needed !(attr, key , label::S, u) where {S<:AbstractString}
222- isempty(label) && return attr[key ] = UnitfulString(label, u)
223- if attr[:plot_object].backend == Plots.PGFPlotsXBackend()
224- attr[key] = UnitfulString(
241+ function append_cbar_unit_if_needed !(attr, label:: S , u) where {S<: AbstractString }
242+ isempty(label) && return attr[:colorbar_title ] = UnitfulString(label, u)
243+ attr[:colorbar_title] = if Plots. backend_name() ≡ :pgfplotsx
244+ UnitfulString(
225245 LaTeXString(
226246 Plots. format_unit_label(
227247 label,
228248 latexify(u),
229- get(attr, Symbol(get(attr, :letter, ""), :unitformat) , :round),
249+ get(attr, :zunitformat , :round),
230250 ),
231251 ),
232252 u,
233253 )
234254 else
235- attr[key] = UnitfulString(
255+ UnitfulString(
236256 S(
237257 Plots. format_unit_label(
238258 label,
239259 u,
240- get(attr, Symbol(get(attr, :letter, ""), :unitformat) , :round),
260+ get(attr, :zunitformat , :round),
241261 ),
242262 ),
243263 u,
@@ -246,9 +266,8 @@ function append_unit_if_needed!(attr, key, label::S, u) where {S<:AbstractString
246266end
247267
248268getaxisunit(:: Nothing ) = nothing
249- getaxisunit(s::UnitfulString) = s.unit
250- getaxisunit(a::Axis) = getaxisunit(a[:unit])
251269getaxisunit(u) = u
270+ getaxisunit(a:: Axis ) = getaxisunit(a[:unit])
252271
253272#= =============
254273Fix annotations
@@ -280,8 +299,10 @@ function Plots.locate_annotation(
280299 rel:: NTuple{N,<:MissingOrQuantity} ,
281300 label,
282301) where {N}
283- units = getaxisunit(sp.attr[:xaxis], sp.attr[:yaxis], sp.attr[:zaxis])
284- Plots.locate_annotation(sp, _ustrip.(zip(units, rel)), label)
302+ units = getaxisunit(sp. attr[:xaxis]),
303+ getaxisunit(sp. attr[:yaxis]),
304+ getaxisunit(sp. attr[:zaxis])
305+ PlotsBase. locate_annotation(sp, _ustrip.(zip(units, rel)), label)
285306end
286307
287308#= =================#
0 commit comments