Skip to content

Commit 232a9ba

Browse files
committed
tweak plots
1 parent a6d6996 commit 232a9ba

File tree

2 files changed

+74
-42
lines changed

2 files changed

+74
-42
lines changed

ext/DimensionalDataMakie.jl

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,29 @@ for f in (:scatter, :lines, :scatterlines, :stairs, :stem, :barplot, :waterfall)
142142
@eval begin
143143
@doc $docstring
144144
function Makie.$f(A::AbstractDimVector; axislegendkw=(;), attributes...)
145-
args, merged_attributes = _pointbased1(A, attributes)
145+
args, merged_attributes = _pointbased1(A; attributes...)
146146
p = Makie.$f(args...; merged_attributes...)
147147
axislegend(p.axis; merge=false, unique=false, axislegendkw...)
148148
return p
149149
end
150150
function Makie.$f!(axis, A::AbstractDimVector; axislegendkw=(;), attributes...)
151-
args, merged_attributes = _pointbased1(A, attributes; set_axis_attributes=false)
151+
args, merged_attributes = _pointbased1(A; attributes..., _set_axis_attributes=false)
152152
return Makie.$f!(axis, args...; merged_attributes...)
153153
end
154154
end
155155
end
156156

157157

158158
function Makie.linesegments(A::AbstractDimVector; axislegendkw=(;), attributes...)
159-
args, merged_attributes = _pointbased1(A, attributes)
159+
args, merged_attributes = _pointbased1(A; attributes...)
160160
lines = _interval_lines(A)
161161
p = Makie.linesegments(lines; merged_attributes...)
162162
axislegend(p.axis; merge=false, unique=false, axislegendkw...)
163163
return p
164164
end
165165

166166
function Makie.linesegments!(x, A::AbstractDimVector; attributes...)
167-
args, merged_attributes = _pointbased1(A, attributes)
167+
args, merged_attributes = _pointbased1(A; attributes...)
168168
lines = _interval_lines(A)
169169
return Makie.linesegments(x, lines; merged_attributes...)
170170
end
@@ -183,15 +183,15 @@ function Makie.plot!(ax, A::AbstractDimVector; kw...)
183183
only(isintervals(A)) ? Makie.linesegments!(ax, A; kw...) : Makie.scatter(axis, A; kw...)
184184
end
185185

186-
function _pointbased1(A, attributes; set_axis_attributes=true)
186+
function _pointbased1(A; _set_axis_attributes=true, attributes...)
187187
# Array/Dimension manipulation
188188
A1 = _prepare_for_makie(A)
189189
lookup_attributes, newdims = _split_attributes(A1)
190190
A2 = _restore_dim_names(set(A1, newdims[1] => newdims[1]), A)
191191
args = Makie.convert_arguments(Makie.PointBased(), A2)
192192
# Plot attribute generation
193193
user_attributes = Makie.Attributes(; attributes...)
194-
axis_attributes = if set_axis_attributes
194+
axis_attributes = if _set_axis_attributes
195195
Attributes(;
196196
axis=(;
197197
xlabel=string(name(dims(A, 1))),
@@ -206,7 +206,7 @@ function _pointbased1(A, attributes; set_axis_attributes=true)
206206
label=DD.label(A),
207207
)
208208
merged_attributes = merge(user_attributes, axis_attributes, plot_attributes, lookup_attributes)
209-
if !set_axis_attributes
209+
if !_set_axis_attributes
210210
delete!(merged_attributes, :axis)
211211
end
212212
return args, merged_attributes
@@ -228,11 +228,8 @@ for f in (:heatmap, :image, :contour, :contourf, :spy, :surface)
228228
"""
229229
@eval begin
230230
@doc $docstring
231-
function Makie.$f(A::AbstractDimMatrix;
232-
x=nothing, y=nothing, colorbarkw=(;), attributes...
233-
) where T
234-
replacements = _keywords2dimpairs(x, y)
235-
A1, A2, args, merged_attributes = _surface2(A, attributes, replacements)
231+
function Makie.$f(A::AbstractDimMatrix{T}; colorbarkw=(;), attributes...) where T
232+
A1, A2, args, merged_attributes = _surface2(A; attributes...)
236233
p = if $(f == :surface)
237234
# surface is an LScene so we cant pass attributes
238235
p = Makie.$f(args...; attributes...)
@@ -250,11 +247,8 @@ for f in (:heatmap, :image, :contour, :contourf, :spy, :surface)
250247
end
251248
return p
252249
end
253-
function Makie.$f!(axis, A::AbstractDimMatrix;
254-
x=nothing, y=nothing, colorbarkw=(;), attributes...
255-
)
256-
replacements = _keywords2dimpairs(x, y)
257-
_, _, args, _ = _surface2(A, attributes, replacements)
250+
function Makie.$f!(axis, A::AbstractDimMatrix; colorbarkw=(;), attributes...)
251+
_, _, args, _ = _surface2(A, attributes)
258252
# No ColourBar in the ! in-place versions
259253
return Makie.$f!(axis, args...; attributes...)
260254
end
@@ -280,23 +274,23 @@ function Makie.plot!(ax, A::AbstractDimMatrix; kw...)
280274
end
281275
end
282276

283-
function Makie.linesegments(A::AbstractDimArray; axislegendkw=(;), attributes...)
284-
# args, merged_attributes = _pointbased1(A, attributes)
277+
function Makie.linesegments(A::AbstractDimMatrix; attributes...)
278+
A1, A2, args, merged_attributes = _surface2(A; attributes...)
285279
lines = vec(collect(DimLines(A)))
286-
p = Makie.linesegments(lines; color=vec(A))#, merged_attributes...)
287-
# axislegend(p.axis; merge=false, unique=false, axislegendkw...)
280+
p = Makie.linesegments(lines; color=vec(A), merged_attributes...)
288281
return p
289282
end
290283

291-
function Makie.linesegments!(x, A::AbstractDimArray; attributes...)
292-
# args, merged_attributes = _pointbased1(A, attributes)
284+
function Makie.linesegments!(x, A::AbstractDimMatrix; attributes...)
285+
A1, A2, args, merged_attributes = _surface2(A; attributes...)
293286
lines = collect(vec(DimLines(A)))
294-
return Makie.linesegments(x, lines; colors=vec(A))#, merged_attributes...)
287+
return Makie.linesegments(x, lines; colors=vec(A), merged_attributes...)
295288
end
296289

297-
function _surface2(A, attributes, replacements)
290+
function _surface2(A; x=nothing, y=nothing, attributes...)
291+
replacements = _keywords2dimpairs(x, y)
298292
# Array/Dimension manipulation
299-
A1 = _prepare_for_makie(A, replacements)
293+
A1 = _prepare_for_makie(A; replacements)
300294
lookup_attributes, newdims = _split_attributes(A1)
301295
A2 = _restore_dim_names(set(A1, map(Pair, newdims, newdims)...), A, replacements)
302296
args = Makie.convert_arguments(Makie.ContinuousSurface(), A2)
@@ -331,16 +325,15 @@ for f in (:volume, :volumeslices)
331325
"""
332326
@eval begin
333327
@doc $docstring
334-
function Makie.$f(A::AbstractDimArray{<:Any,3}; x=nothing, y=nothing, z=nothing, attributes...)
328+
function Makie.$f(A::AbstractDimArray{<:Any,3}; attributes...)
335329
replacements = _keywords2dimpairs(x, y, z)
336-
A1, A2, args, merged_attributes = _volume3(A, attributes, replacements)
330+
A1, A2, args, merged_attributes = _volume3(A; attributes...)
337331
p = Makie.$f(args...; merged_attributes...)
338332
p.axis.scene[OldAxis][:names, :axisnames] = map(DD.label, DD.dims(A2))
339333
return p
340334
end
341-
function Makie.$f!(axis, A::AbstractDimArray{<:Any,3}; x=nothing, y=nothing, z=nothing, attributes...)
342-
replacements = _keywords2dimpairs(x, y, z)
343-
_, _, args, _ = _volume3(A, attributes, replacements)
335+
function Makie.$f!(axis, A::AbstractDimArray{<:Any,3}; attributes...)
336+
_, _, args, _ = _volume3(A; attributes...)
344337
return Makie.$f!(axis, args...; attributes...)
345338
end
346339
end
@@ -369,27 +362,47 @@ function Makie.plot!(ax, A::AbstractDimArray{<:Any,3}; kw...)
369362
end
370363
end
371364

372-
function Makie.poly(A::AbstractDimArray; axislegendkw=(;), attributes...)
373-
# args, merged_attributes = _pointbased1(A, attributes)
365+
function Makie.poly(A::AbstractDimArray{<:Any,3}; attributes...)
366+
A1, A2, args, merged_attributes = _volume3(A; attributes...)
374367
dps = DimPolygons(A)
375368
polys = vec(collect(dps))
376369
# TODO this doesn't plot properly?
377370
# All polygons plat on the same plane
378-
p = Makie.poly(polys; color=vec(A))#, merged_attributes...)
379-
axislegend(p.axis; merge=false, unique=false, axislegendkw...)
371+
p = Makie.poly(polys; color=vec(A2), merged_attributes...)
380372
return p
381373
end
382374

383-
function Makie.poly!(x, A::AbstractDimArray; attributes...)
384-
# args, merged_attributes = _pointbased1(A, attributes)
385-
polys = collect(vec(DimPolygons(A)))
386-
return Makie.poly(x, polys; colors=vec(A))#, merged_attributes...)
375+
function Makie.poly!(ax, A::AbstractDimArray{<:Any,3}; attributes...)
376+
A1, A2, args, merged_attributes = _volume3(A; attributes...)
377+
polys = vec(collect(DimPolygons(A2)))
378+
return Makie.poly(ax, polys; color=vec(A), merged_attributes...)
379+
end
380+
381+
function Makie.linesegments(A::AbstractDimArray{<:Any,3}; attributes...)
382+
A1, A2, args, merged_attributes = _volume3(A; attributes...)
383+
lines = vec(collect(DimLines(A)))
384+
p = Makie.linesegments(lines; color=vec(A), merged_attributes...)
385+
return p
386+
end
387+
388+
function Makie.linesegments!(ax, A::AbstractDimArray{<:Any,3}; attributes...)
389+
A1, A2, args, merged_attributes = _surface2(A; attributes...)
390+
lines = collect(vec(DimLines(A)))
391+
return Makie.linesegments!(ax, lines; colors=vec(A), merged_attributes...)
392+
end
393+
394+
function Makie.scatter(A::AbstractDimArray{<:Any,3}; attributes...)
395+
A1, A2, args, merged_attributes = _volume3(A; attributes...)
396+
points = vec(collect(DimPoints(A2)))
397+
p = Makie.scatter(points; color=vec(A), merged_attributes...)
398+
return p
387399
end
388400

389401

390-
function _volume3(A, attributes, replacements)
402+
function _volume3(A; x=nothing, y=nothing, z=nothing, attributes...)
403+
replacements = _keywords2dimpairs(x, y, z)
391404
# Array/Dimension manipulation
392-
A1 = _prepare_for_makie(A, replacements)
405+
A1 = _prepare_for_makie(A; replacements)
393406
_, newdims = _split_attributes(A1)
394407
A2 = _restore_dim_names(set(A1, map(Pair, newdims, newdims)...), A, replacements)
395408
args = Makie.convert_arguments(Makie.VolumeLike(), A2)
@@ -608,7 +621,7 @@ function _split_attributes(dim::Dimension)
608621
return attributes, dims[1]
609622
end
610623

611-
function _prepare_for_makie(A, replacements=())
624+
function _prepare_for_makie(A; replacements=())
612625
_permute_xyz(maybeshiftlocus(Center(), A; dims=(XDim, YDim)), replacements) |> _reorder
613626
end
614627

test/plotrecipes.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ nothing
157157
# da_im2 = DimArray(im2, (X(10:10:100), Y(10:10:100)), "Image")
158158
# da_im2 |> plot
159159

160+
using DimensionalData
160161
using CairoMakie: CairoMakie as M
161162
using ColorTypes
162163
@testset "Makie" begin
@@ -200,6 +201,11 @@ using ColorTypes
200201
M.waterfall!(ax, A1)
201202
fig, ax, _ = M.waterfall(A1m)
202203
M.waterfall!(ax, A1m)
204+
205+
A1intervals = rand(X(1.0:10.0; sampling=Intervals(Start())); name=:test)
206+
A1intervals
207+
M.plot(set(A1intervals, X=>Points()))
208+
M.plot(A1intervals)
203209
# 2d
204210
A2 = rand(X(10:10:100), Y(['a', 'b', 'c']))
205211
A2r = rand(Y(10:10:100), X(['a', 'b', 'c']))
@@ -251,6 +257,10 @@ using ColorTypes
251257
M.series!(ax, A2m)
252258
@test_throws ArgumentError M.plot(A2; y=:c)
253259
@test_throws ArgumentError M.plot!(ax, A2; y=:c)
260+
A2intervals1 = rand(X(10:10:100; sampling=Intervals(Start())), Y(1:3))
261+
M.plot(A2intervals1)
262+
A2intervals2 = rand(X(10:10:100; sampling=Intervals(Start())), Y(1:3; sampling=Intervals(Start())))
263+
M.plot(A2intervals2)
254264

255265
# x/y can be specified
256266
A2ab = DimArray(rand(6, 10), (:a, :b); name=:stuff)
@@ -281,6 +291,7 @@ using ColorTypes
281291

282292
# 3d
283293
A3 = rand(X(7), Z(10), Y(5))
294+
M.plot(A3)
284295
A3m = rand([missing, (1:7)...], X(7), Z(10), Y(5))
285296
A3m[3] = missing
286297
A3rgb = rand(RGB, X(7), Z(10), Y(5))
@@ -304,4 +315,12 @@ using ColorTypes
304315
fig, ax, _ = M.volumeslices(A3abc; x=:c)
305316
fig, ax, _ = M.volumeslices(A3abc; z=:a)
306317
M.volumeslices!(ax, A3abc;z=:a)
318+
319+
A3intervals1 = rand(X(10:1:20; sampling=Intervals(Start())), Y(1:3), Z(10:20))
320+
M.plot(A3intervals1)
321+
# Broken from here
322+
A3intervals2 = rand(X(10:1:20; sampling=Intervals(Start())), Y(1:3), Z(10:20; sampling=Intervals(Start())))
323+
M.plot(A3intervals2)
324+
A3intervals2a = rand(X(10:1:10; sampling=Intervals(Start())), Y(1:1; sampling=Intervals(Start())), Z(10:20))
325+
M.plot(A3intervals2a)
307326
end

0 commit comments

Comments
 (0)