-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathplotting.jl
More file actions
540 lines (486 loc) · 15.7 KB
/
Copy pathplotting.jl
File metadata and controls
540 lines (486 loc) · 15.7 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# Plotting examples
# =================
# ## Plotters
# All plot functions in GridVisualize.jl have a `Plotter` keyword argument
# which defaults to `nothing`. This allows to pass a module as plotting backend
# without creating a dependency. Fully supported are `PyPlot`, `GLMakie` and `PlutoVista`.
# `WGLMakie` and `CairoMakie` work in principle but in the moment don't deliver
# all necessary functionality. For `Plots` we miss the possibility to work with triangle meshes (this is under development, though)
#
# Also supported is [`VTKView`](https://github.com/j-fu/VTKView.jl) which is experimental and works only on linux.
#
#
# ## Grid plots
# Here, we define some sample grids for plotting purposes.
using ExtendableGrids
using GridVisualize
using Triangulate: Triangulate, TriangulateIO, triangulate
using Printf
# ### 1D grids
# See [`GridVisualize.gridplot`](@@ref).
function grid1d(; n = 50)
X = collect(0:(1 / n):1)
return g = simplexgrid(X)
end
function plotting_grid1d(; Plotter = default_plotter(), kwargs...)
return gridplot(grid1d(); Plotter = Plotter, resolution = (500, 200), kwargs...)
end
# 
# ### 2D grids
# See [`GridVisualize.gridplot`](@@ref).
function grid2d(; n = 20)
X = collect(0:(1 / n):1)
return g = simplexgrid(X, X)
end
function plotting_grid2d(; Plotter = default_plotter(), kwargs...)
return gridplot(grid2d(); Plotter = Plotter, kwargs...)
end
# 
# ### 3D grids
# See [`GridVisualize.gridplot`](@@ref).
# The kwargs `xplane`, `yplane` and `zplane` which allow to control
# cutplanes which peel off some elements from the grid in 3d and allow to
# explore the inner triangulation.
#
# For Makie and VTKView, the cutplane values can be controlled interactively.
function grid3d(; n = 15)
X = collect(0:(1 / n):1)
return g = simplexgrid(X, X, X)
end
function plotting_grid3d(; Plotter = default_plotter(), kwargs...)
return gridplot(grid3d(); Plotter = Plotter, kwargs...)
end
# 
# ## Function plots
# ### Function on 1D grid
# See [`GridVisualize.scalarplot`](@@ref).
function func1d(; n = 50)
g = grid1d(; n = n)
return g, map(x -> sinpi(2 * x[1]), g)
end
function plotting_func1d(; Plotter = default_plotter(), kwargs...)
g, f = func1d()
return scalarplot(g, f; Plotter = Plotter, resolution = (500, 300), kwargs...)
end
# 
# ### Function on 2D grid
# See [`GridVisualize.scalarplot`](@@ref).
function func2d(; n = 30)
g = grid2d(; n = n)
return g, map((x, y) -> sinpi(2 * x) * sinpi(3.5 * y), g)
end
function plotting_func2d(; Plotter = default_plotter(), kwargs...)
g, f = func2d()
return scalarplot(g, f; Plotter = Plotter, levels = 10, kwargs...)
end
# 
# ### Function on 3D grid
# See [`GridVisualize.scalarplot`](@@ref).
#
# `xplane`, `yplane` and `zplane` now define cut planes where
# the function projection is plotted as a heatmap.
# The additional `flevel` keyword argument allows
# to control an isolevel.
#
# For Makie and VTKView, the cutplane values and the flevel can be controlled interactively.
function func3d(; n = 15)
g = grid3d(; n = n)
return g, map((x, y, z) -> sinpi(2 * x) * sinpi(3.5 * y) * sinpi(1.5 * z), g)
end
function plotting_func3d(;
Plotter = default_plotter(),
xplanes = [0.49],
yplanes = [0.49],
zplanes = [0.49],
levels = 5,
kwargs...,
)
g, f = func3d()
return scalarplot(g, f; Plotter = Plotter, levels, xplanes, yplanes, zplanes, kwargs...)
end
# 
# ## d-1 dim slice in d-dim data
# ### 2D slice of a 3D grid
# See [`GridVisualize.scalarplot`](@@ref).
#
# You can plot a 2D slice of a function defined on a
# 3D grid by providing a `slice` key word argument which
# describes a plane equation of a fixed value to one axis.
#
# The example shows a plot for a fixed axis y = 0.5.
# Note that labeling the other axes may be useful
function plotting_slice3d(;
Plotter = default_plotter(),
slice = :y => 0.5,
xlabel = "x",
ylabel = "z",
kwargs...,
)
g, f = func3d()
return scalarplot(g, f; Plotter, slice, xlabel, ylabel, kwargs...)
end
# 
# ### 1D line of a 2D grid
#
# See [`GridVisualize.scalarplot`](@@ref).
# You can plot a 1D line of a function defined on a
# 2D grid by providing a `slice` key word argument which
# describes a line equation of a fixed value to one axis.
#
# The example shows a plot along the diagonal x + y - 1 = 0
# Note that you should provide meaningful axes labels
function plotting_line2d(;
Plotter = default_plotter(),
slice = :(x + y - 1),
xlabel = "line",
ylabel = "value",
kwargs...,
)
g, f = func2d()
return scalarplot(g, f; Plotter, slice, xlabel, ylabel, kwargs...)
end
# 
# ## Vector and stream plots
# ### 2D vector
# See [`GridVisualize.vectorplot`](@@ref).
function vec2d(; n = 20)
g = grid2d(; n = n)
return g,
vcat(
map((x, y) -> sinpi(2 * x) * sinpi(3.5 * y), g)',
map((x, y) -> cospi(2 * x) * cospi(3.5 * y), g)'
)
end
function plotting_vec2d(; Plotter = default_plotter(), n = 20, kwargs...)
g, f = vec2d(; n = n)
return vectorplot(g, f; Plotter = Plotter, kwargs...)
end
# 
# ### 2D stream
# See [`GridVisualize.streamplot`](@@ref).
# Stream plots are currently only available with PyPlot and Makie
function plotting_stream2d(; Plotter = default_plotter(), n = 50, kwargs...)
g, f = vec2d(; n = n)
return GridVisualize.streamplot(g, f; Plotter = Plotter, rasterpoints = 20, kwargs...)
end
# 
# ## Animation
# Animation examples work in the REPL. They are useful for in-situ visualization
# of evolving calculation data. For the creation of videos/movies, see the [Movie](@@ref) example.
# ### Animation of function on 1D grid
function anim_func1d(; Plotter = default_plotter(), kwargs...)
step = 0.01
if plottertype(Plotter) <: UnionMakieType
step = 0.0001
end
g = grid1d(; n = 50)
vis = GridVisualizer(; Plotter, kwargs...)
for t in 0:step:1
f = map(x -> sinpi(3 * (x[1] - t)), g)
scalarplot!(vis, g, f, title = "t=$(t)", show = true)
end
return nothing
end
# ### Animation of function on 2D grid
function anim_func2d(; Plotter = default_plotter(), kwargs...)
step = 0.05
if plottertype(Plotter) <: UnionMakieType
step = 0.005
end
g = grid2d(; n = 30)
vis = GridVisualizer(; Plotter, kwargs...)
for t in 0:step:1
f = map(x -> sinpi(3 * (x[1] - t) * (2 * x[2] - 2t)), g)
scalarplot!(vis, g, f, title = "t=$(t)", show = true)
end
return nothing
end
# ### Animation of function on 3D grid
function anim_func3d(; Plotter = default_plotter(), kwargs...)
step = 0.05
if plottertype(Plotter) <: UnionMakieType
step = 0.005
end
g = grid3d(; n = 15)
vis = GridVisualizer(; Plotter, kwargs...)
for t in 0:step:1
f = map(x -> sinpi(3 * (x[1] - t)) * sinpi(2 * x[2] - 2t) * cospi(x[3] - t / 2), g)
scalarplot!(vis, g, f, title = "t=$(t)", show = true)
end
return nothing
end
# ## Movie
# Movies can contain any of the previous plots.
# Best created with GLMakie or CairoMakie. File formats are gif and mp4.
# See [`GridVisualize.movie`](@@ref)
# See the [Animation](@@ref) examples
# for in-situ visualization of calculation results in the REPL.
function plotting_movie(; filename = "plotting_video.gif", Plotter = default_plotter())
vis = GridVisualizer(; Plotter = Plotter, size = (600, 200), layout = (1, 2))
X = 0:0.2:10
grid = simplexgrid(X, X)
return movie(vis; file = filename) do vis
for t in 0:0.1:10
f = map((x, y) -> sin(x - t) * cos(y - t), grid)
g = map((x, y) -> sin(t) * sin(x) * cos(y), grid)
scalarplot!(
vis[1, 1],
grid,
f;
clear = true,
title = "t=$(t)",
limits = (-1, 1),
levels = 7,
colormap = :hot
)
scalarplot!(
vis[1, 2],
grid,
g;
clear = true,
title = "t=$(t)",
limits = (-1, 1),
levels = 7,
colormap = :hot
)
reveal(vis)
end
end
end
# 
# ## Multiscene plots
# We can combine multiple plots into one scene according to
# some layout grid given by the layout parameter.
#
# This is not currently supported by the PlutVista backend.
#
# The ',' key for GLMakie and the '*' key for VTKView allow to
# switch between gallery view (default) and focused view of only
# one subscene.
function plotting_multiscene!(p)
gridplot!(p[1, 1], grid1d(); title = "1D grid", legend = :rt)
scalarplot!(
p[2, 1],
grid1d(),
sin;
title = "1D grid function",
label = "sin",
markershape = :diamond,
color = :red,
legend = :rb,
)
scalarplot!(
p[2, 1],
grid1d(),
cos;
title = "1D grid function",
label = "cos",
linestyle = :dash,
markershape = :none,
color = :green,
clear = false,
)
gridplot!(p[1, 2], grid2d(); title = "2D grid")
scalarplot!(p[2, 2], func2d()...; colormap = :bamako, title = "2D grid function")
gridplot!(p[1, 3], grid3d(); zplane = 0.49, title = "3D grid")
scalarplot!(
p[2, 3],
func3d()...;
zplane = 0.49,
flevel = 0.5,
colormap = :bamako,
title = "3D grid function",
)
vectorplot!(p[1, 4], vec2d()...; title = "2D quiver")
GridVisualize.streamplot!(p[2, 4], vec2d()...; title = "2D stream")
return reveal(p)
end
function plotting_multiscene(; Plotter = default_plotter(), resolution = (1000, 500))
return plotting_multiscene!(
GridVisualizer(;
Plotter = Plotter,
layout = (2, 4),
clear = true,
resolution = resolution,
)
)
end
# 
# ## Plots of functions on subgrids
# We can jointly plot functions on different subgrids which
# e.g. model a particle density jumping at a heterointerface
# Currently supported for PyPlot and Makie
#
# The general scheme is to pass a vector of subgrid, the parent grid and the corresponding
# vector of functions on the respective subgrids
#
# ### 1D case
function plotting_jfunc1d(; Plotter = default_plotter(), filename = "plotting_jfunc1d.gif")
X = 0:1:10
g = simplexgrid(X)
cellmask!(g, [0], [5], 2)
g1 = subgrid(g, [1])
g2 = subgrid(g, [2])
vis = GridVisualizer(; Plotter, color = :red)
return movie(vis; file = filename) do vis
for t in 0:0.05:1
func1 = map((x) -> x - t, g1)
func2 = map((x) -> -x + t, g2)
func = map(x -> x^2 / 100 - t, g)
scalarplot!(
vis,
[g1, g2],
g,
[func1, func2];
Plotter,
elevation = 0.1,
clear = true,
color = :red,
)
scalarplot!(
vis,
g,
func;
Plotter,
elevation = 0.1,
clear = false,
color = :green,
)
reveal(vis)
end
end
end
# 
# ### 2D case
function plotting_jfunc2d(; Plotter = default_plotter(), kwargs...)
X = 0:0.7:10
g = simplexgrid(X, X)
cellmask!(g, [0, 0], [5, 5], 2)
g1 = subgrid(g, [1])
g2 = subgrid(g, [2])
func1 = map((x, y) -> x^2 + y, g1)
func2 = map((x, y) -> (x + y^2), g2)
return scalarplot([g1, g2], g, [func1, func2]; Plotter, kwargs...)
end
# 
# ### 3D case
function plotting_jfunc3d(;
Plotter = default_plotter(),
levels = 0,
yplane = 0.25,
xplane = 0.25,
zplane = 0.25,
levelalpha = 1,
colormap = :hot,
kwargs...,
)
X = 0:0.1:1
g = simplexgrid(X, X, X)
cellmask!(g, [0, 0, 0], [0.5, 0.5, 0.5], 2)
g1 = subgrid(g, [1])
g2 = subgrid(g, [2])
func1 = map((x, y, z) -> (x + y + z), g1)
func2 = map((x, y, z) -> (3 - x - y - z), g2)
return scalarplot(
[g1, g2],
g,
[func1, func2];
Plotter,
levels,
xplane,
yplane,
zplane,
levelalpha,
colormap,
kwargs...,
)
end
# 
# ## Custom plots
# See [`GridVisualize.customplot!`](@@ref)
function plotting_custom(; Plotter = default_plotter(), kwargs...)
vis = GridVisualizer(; Plotter = Plotter)
grid = grid2d()
gridplot!(vis, grid)
customplot!(vis) do ax
plottertype(Plotter) <: UnionMakieType && Plotter.scatter!(ax, rand(10), rand(10), fill(0.1, 10); color = :blue, markersize = 20)
plottertype(Plotter) <: PyPlotType && ax.scatter(rand(10), rand(10); s = 500)
plottertype(Plotter) <: PlotsType && Plotter.scatter!(ax, rand(10), rand(10); color = :blue, markersize = 10, label = nothing)
end
return reveal(vis)
end
# 
# ## TriangulateIO
#
# GridVisualize comes with [`GridVisualize.plot_triangulateio`](@@ref), a method to plot the input/output
# struct of the Triangle mesh generator, provided by the [Triangulate.jl](https://github.com/JuliaGeometry/Triangulate.jl) wrapper.
# Supported are PyPlot and Makie backends.
#
#
function plotting_triangulateio(; Plotter = default_plotter(), maxarea = 0.05, resolution = (600, 300))
triin = Triangulate.TriangulateIO()
triin.pointlist = Matrix{Cdouble}([0.0 0.0; 1.0 0.0; 1.0 1.0; 0.6 0.6; 0.0 1.0]')
triin.segmentlist = Matrix{Cint}([1 2; 2 3; 3 4; 4 5; 5 1]')
triin.segmentmarkerlist = Vector{Int32}([1, 2, 3, 4, 5])
area = @sprintf("%.15f", maxarea)
(triout, vorout) = triangulate("pa$(area)DQ", triin)
vis = GridVisualizer(;
Plotter, layout = (1, 2), clear = true, resolution
)
plot_triangulateio!(vis[1, 1], triin, title = "input")
plot_triangulateio!(
vis[1, 2],
triout;
voronoi = vorout,
circumcircles = true,
title = "output"
)
return reveal(vis)
end;
# 
plotting_functions_png = [
:plotting_multiscene,
:plotting_func1d,
:plotting_func2d,
:plotting_func3d,
:plotting_slice3d,
:plotting_line2d,
:plotting_jfunc2d,
:plotting_jfunc3d,
:plotting_vec2d,
:plotting_stream2d,
:plotting_grid1d,
:plotting_grid2d,
:plotting_grid3d,
:plotting_custom,
:plotting_triangulateio,
]
plotting_functions_gif = [
:plotting_jfunc1d,
:plotting_movie,
]
function generateplots(picdir; Plotter = nothing)
filepaths = String[]
if isdefined(Plotter, :Makie)
size = (600, 300)
Plotter.activate!(; type = "png", visible = false)
for plotting_f in plotting_functions_png
@eval begin
path = joinpath($picdir, "$($plotting_f).png")
p = $plotting_f(; Plotter = $Plotter)
$Plotter.save(path, p)
println("successfully generated plot for $($plotting_f)")
push!($filepaths, path)
end
end
for plotting_f in plotting_functions_gif
@eval begin
path = joinpath($picdir, "$($plotting_f).gif")
p = $plotting_f(; Plotter = $Plotter, filename = path)
println("successfully generated plot for $($plotting_f)")
push!($filepaths, path)
end
end
end
return filepaths
end