forked from MakieOrg/Makie.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makielayout.jl
273 lines (246 loc) · 9.82 KB
/
makielayout.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Minimal sanity checks for Makie Layout
@testset "Blocks constructors" begin
fig = Figure()
ax = Axis(fig[1, 1])
cb = Colorbar(fig[1, 2])
gl2 = fig[2, :] = Makie.GridLayout()
bu = gl2[1, 1] = Button(fig)
sl = gl2[1, 2] = Slider(fig)
scat = scatter!(ax, rand(10))
le = gl2[1, 3] = Legend(fig, [scat], ["scatter"])
to = gl2[1, 4] = Toggle(fig)
te = fig[0, :] = Label(fig, "A super title")
me = fig[end + 1, :] = Menu(fig, options=["one", "two", "three"])
tb = fig[end + 1, :] = Textbox(fig)
is = fig[end + 1, :] = IntervalSlider(fig)
@test true
end
@testset "deleting from axis" begin
f = Figure()
ax = Axis(f[1, 1])
sc = scatter!(ax, randn(100, 2))
li = lines!(ax, randn(100, 2))
hm = heatmap!(ax, randn(20, 20))
# axis contains 3 + 1 plots, one for the zoomrectangle
@test length(ax.scene.plots) == 4
delete!(ax, sc)
@test length(ax.scene.plots) == 3
@test sc ∉ ax.scene.plots
empty!(ax)
@test isempty(ax.scene.plots)
end
@testset "zero heatmap" begin
xs = LinRange(0, 20, 10)
ys = LinRange(0, 15, 10)
zs = zeros(length(xs), length(ys))
fig = Figure()
_, hm = heatmap(fig[1, 1], xs, ys, zs)
cb = Colorbar(fig[1, 2], hm)
@test hm.attributes[:colorrange][] == (-.5, .5)
@test cb.limits[] == (-.5, .5)
hm.attributes[:colorrange][] = Float32.((-1, 1))
@test cb.limits[] == (-1, 1)
# TODO: This doesn't work anymore because colorbar doesn't use the same observable
# cb.limits[] = Float32.((-2, 2))
# @test hm.attributes[:colorrange][] == (-2, 2)
end
@testset "Axis limits basics" begin
f = Figure()
ax = Axis(f[1, 1], limits = (nothing, nothing))
ax.targetlimits[] = BBox(0, 10, 0, 20)
@test ax.finallimits[] == BBox(0, 10, 0, 20)
@test ax.limits[] == (nothing, nothing)
xlims!(ax, -10, 10)
@test ax.limits[] == ((-10, 10), nothing)
@test ax.finallimits[] == BBox(-10, 10, 0, 20)
ylims!(ax, -20, 30)
@test ax.limits[] == ((-10, 10), (-20, 30))
@test ax.finallimits[] == BBox(-10, 10, -20, 30)
limits!(ax, -5, 5, -10, 10)
@test ax.finallimits[] == BBox(-5, 5, -10, 10)
@test ax.limits[] == ((-5, 5), (-10, 10))
ax.limits[] = (nothing, nothing)
ax.xautolimitmargin = (0, 0)
ax.yautolimitmargin = (0, 0)
scatter!(Point2f[(0, 0), (1, 2)])
reset_limits!(ax)
@test ax.limits[] == (nothing, nothing)
@test ax.targetlimits[] == BBox(0, 1, 0, 2)
@test ax.finallimits[] == BBox(0, 1, 0, 2)
scatter!(Point2f(3, 4))
reset_limits!(ax)
@test ax.limits[] == (nothing, nothing)
@test ax.targetlimits[] == BBox(0, 3, 0, 4)
@test ax.finallimits[] == BBox(0, 3, 0, 4)
limits!(ax, -1, 1, 0, 2)
@test ax.limits[] == ((-1, 1), (0, 2))
@test ax.targetlimits[] == BBox(-1, 1, 0, 2)
@test ax.finallimits[] == BBox(-1, 1, 0, 2)
scatter!(Point2f(5, 6))
reset_limits!(ax)
@test ax.limits[] == ((-1, 1), (0, 2))
@test ax.targetlimits[] == BBox(-1, 1, 0, 2)
@test ax.finallimits[] == BBox(-1, 1, 0, 2)
autolimits!(ax)
@test ax.limits[] == (nothing, nothing)
@test ax.targetlimits[] == BBox(0, 5, 0, 6)
@test ax.finallimits[] == BBox(0, 5, 0, 6)
xlims!(ax, [-10, 10])
@test ax.limits[] == ([-10, 10], nothing)
@test ax.targetlimits[] == BBox(-10, 10, 0, 6)
@test ax.finallimits[] == BBox(-10, 10, 0, 6)
scatter!(Point2f(11, 12))
reset_limits!(ax)
@test ax.limits[] == ([-10, 10], nothing)
@test ax.targetlimits[] == BBox(-10, 10, 0, 12)
@test ax.finallimits[] == BBox(-10, 10, 0, 12)
autolimits!(ax)
ylims!(ax, [5, 7])
@test ax.limits[] == (nothing, [5, 7])
@test ax.targetlimits[] == BBox(0, 11, 5, 7)
@test ax.finallimits[] == BBox(0, 11, 5, 7)
scatter!(Point2f(-5, -7))
reset_limits!(ax)
@test ax.limits[] == (nothing, [5, 7])
@test ax.targetlimits[] == BBox(-5, 11, 5, 7)
@test ax.finallimits[] == BBox(-5, 11, 5, 7)
end
@testset "Colorbar plot object kwarg clash" begin
for attr in (:colormap, :limits)
f, ax, p = scatter(1:10, 1:10, color = 1:10, colorrange = (1, 10))
Colorbar(f[2, 1], p)
@test_throws ErrorException Colorbar(f[2, 1], p; Dict(attr => nothing)...)
end
for attr in (:colormap, :limits, :highclip, :lowclip)
for F in (heatmap, contourf)
f, ax, p = F(1:10, 1:10, randn(10, 10))
Colorbar(f[1, 2], p)
@test_throws ErrorException Colorbar(f[1, 3], p; Dict(attr => nothing)...)
end
end
end
@testset "Tick functions" begin
automatic = Makie.automatic
Automatic = Makie.Automatic
get_ticks = Makie.get_ticks
get_tickvalues = Makie.get_tickvalues
get_ticklabels = Makie.get_ticklabels
for func in [identity, log, log2, log10, Makie.logit]
tup = ([1, 2, 3], ["a", "b", "c"])
@test get_ticks(tup, func, automatic, 0, 5) == tup
rng = 1:5
@test get_ticks(rng, func, automatic, 0, 5) == ([1, 2, 3, 4, 5], ["1", "2", "3", "4", "5"])
numbers = [1.0, 1.5, 2.0]
@test get_ticks(numbers, func, automatic, 0, 5) == (numbers, ["1.0", "1.5", "2.0"])
@test get_ticks(numbers, func, xs -> string.(xs) .* "kg", 0, 5) == (numbers, ["1.0kg", "1.5kg", "2.0kg"])
@test get_ticks(WilkinsonTicks(5), identity, automatic, 1, 5) == ([1, 2, 3, 4, 5], ["1", "2", "3", "4", "5"])
end
end
@testset "Colorbars" begin
fig = Figure()
hmap = heatmap!(Axis(fig[1, 1]), rand(4, 4))
cb1 = Colorbar(fig[1,2], hmap; height = Relative(0.65))
@test cb1.height[] == Relative(0.65)
@testset "conversion" begin
# https://github.com/MakieOrg/Makie.jl/issues/2278
fig = Figure()
cbar = Colorbar(fig[1,1], colormap=:viridis, colorrange=Vec2f(0, 1))
ticklabel_strings = first.(cbar.axis.elements[:ticklabels][1][])
@test ticklabel_strings[1] == "0.0"
@test ticklabel_strings[end] == "1.0"
end
end
@testset "cycling" begin
fig = Figure()
ax = Axis(fig[1, 1], palette = (patchcolor = [:blue, :green],))
pl = density!(rand(10); color = Cycled(2))
@test pl.color[] == :green
pl = density!(rand(10); color = Cycled(1))
@test pl.color[] == :blue
end
@testset "briefly empty ticklabels" begin
# issue 2079, for some reason at Axis initialization briefly there would be a zero-element
# ticklabel/position array and this would be split into a Vector{Any} for the positions,
# triggering a conversion error
# So we just check that the same scenario doesn't error again
f = Figure()
ax = Axis(f[1,1], xticks = 20:10:80)
scatter!(ax, 30:10:100, rand(Float64, 8), color = :red)
end
# issues 1958 and 2006
@testset "axislegend number align" begin
f = Figure()
ax = Axis(f[1,1], xticks = 20:10:80)
lines!(ax, 1:10, label = "A line")
leg = axislegend(ax, position = (0.4, 0.8))
@test leg.halign[] == 0.4
@test leg.valign[] == 0.8
end
# issue 2005
@testset "invalid plotting function keyword arguments" begin
for T in [Axis, Axis3, LScene]
f = Figure()
kw = (; backgroundcolor = :red)
@test_throws ArgumentError lines(f[1, 1], 1:10, figure = kw)
@test_nowarn lines(f[1, 2], 1:10, axis = kw)
@test_throws ArgumentError lines(f[1, 3][1, 1], 1:10, figure = kw)
@test_nowarn lines(f[1, 4][1, 2], 1:10, axis = kw)
ax = T(f[1, 5])
@test_throws ArgumentError lines!(ax, 1:10, axis = kw)
@test_throws ArgumentError lines!(ax, 1:10, axis = kw)
@test_throws ArgumentError lines!(1:10, axis = kw)
@test_throws ArgumentError lines!(1:10, figure = kw)
@test_nowarn lines!(1:10)
@test_throws ArgumentError lines!(f[1, 5], 1:10, figure = kw)
@test_throws ArgumentError lines!(f[1, 5], 1:10, axis = kw)
@test_nowarn lines!(f[1, 5], 1:10)
end
end
@testset "Linked axes" begin
# this tests a bug in 0.17.4 where the first axis targetlimits
# don't change because the second axis has limits contained inside those
# of the first, so the axis linking didn't proliferate
f = Figure()
ax1 = Axis(f[1, 1], xautolimitmargin = (0, 0), yautolimitmargin = (0, 0))
ax2 = Axis(f[2, 1], xautolimitmargin = (0, 0), yautolimitmargin = (0, 0))
scatter!(ax1, 1:5, 2:6)
scatter!(ax2, 2:3, 3:4)
reset_limits!(ax1)
reset_limits!(ax2)
@test first.(extrema(ax1.finallimits[])) == (1, 5)
@test last.(extrema(ax1.finallimits[])) == (2, 6)
@test first.(extrema(ax2.finallimits[])) == (2, 3)
@test last.(extrema(ax2.finallimits[])) == (3, 4)
linkxaxes!(ax1, ax2)
@test first.(extrema(ax1.finallimits[])) == (1, 5)
@test last.(extrema(ax1.finallimits[])) == (2, 6)
@test first.(extrema(ax2.finallimits[])) == (1, 5)
@test last.(extrema(ax2.finallimits[])) == (3, 4)
linkyaxes!(ax1, ax2)
@test first.(extrema(ax1.finallimits[])) == (1, 5)
@test last.(extrema(ax1.finallimits[])) == (2, 6)
@test first.(extrema(ax2.finallimits[])) == (1, 5)
@test last.(extrema(ax2.finallimits[])) == (2, 6)
end
# issue 1718
@testset "Linked axes of linked axes" begin
# check that if linking axis A and B, where B has already been linked to C, A and C are also linked
f = Figure()
ax1 = Axis(f[1, 1])
ax2 = Axis(f[1, 2])
ax3 = Axis(f[1, 3])
linkaxes!(ax2, ax3)
@test Set(ax1.xaxislinks) == Set([])
@test Set(ax2.xaxislinks) == Set([ax3])
@test Set(ax3.xaxislinks) == Set([ax2])
@test Set(ax1.yaxislinks) == Set([])
@test Set(ax2.yaxislinks) == Set([ax3])
@test Set(ax3.yaxislinks) == Set([ax2])
linkaxes!(ax1, ax2)
@test Set(ax1.xaxislinks) == Set([ax2, ax3])
@test Set(ax2.xaxislinks) == Set([ax1, ax3])
@test Set(ax3.xaxislinks) == Set([ax1, ax2])
@test Set(ax1.yaxislinks) == Set([ax2, ax3])
@test Set(ax2.yaxislinks) == Set([ax1, ax3])
@test Set(ax3.yaxislinks) == Set([ax1, ax2])
end