-
Notifications
You must be signed in to change notification settings - Fork 214
/
test_plot3d.py
485 lines (436 loc) · 11.3 KB
/
test_plot3d.py
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
"""
Test Figure.plot3d.
"""
import os
from pathlib import Path
import numpy as np
import pytest
from pygmt import Figure
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
@pytest.fixture(scope="module", name="data")
def fixture_data():
"""
Load the point data from the test file.
"""
return np.loadtxt(POINTS_DATA)
@pytest.fixture(scope="module", name="region")
def fixture_region():
"""
The data region.
"""
return [10, 70, -5, 10, 0, 1]
@pytest.mark.mpl_image_compare
def test_plot3d_red_circles_zscale(data, region):
"""
Plot the 3-D data in red circles passing in vectors and setting zscale=5.
"""
fig = Figure()
fig.plot3d(
x=data[:, 0],
y=data[:, 1],
z=data[:, 2],
zscale=5,
perspective=[225, 30],
region=region,
projection="X10c",
style="c0.2c",
fill="red",
frame=["afg", "zafg"],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_red_circles_zsize(data, region):
"""
Plot the 3-D data in red circles passing in vectors and setting zsize="6c".
"""
fig = Figure()
fig.plot3d(
x=data[:, 0],
y=data[:, 1],
z=data[:, 2],
zsize="6c",
perspective=[225, 30],
region=region,
projection="X10c",
style="c0.2c",
fill="red",
frame=["afg", "zafg"],
)
return fig
def test_plot3d_fail_1d_array_with_data(data, region):
"""
Should raise an exception if array fill, size, intensity and transparency
are used with matrix.
"""
fig = Figure()
kwargs = {"data": data, "region": region, "projection": "X10c", "frame": "afg"}
with pytest.raises(GMTInvalidInput):
fig.plot3d(style="c0.2c", fill=data[:, 2], **kwargs)
with pytest.raises(GMTInvalidInput):
fig.plot3d(style="cc", size=data[:, 2], fill="red", **kwargs)
with pytest.raises(GMTInvalidInput):
fig.plot3d(style="cc", intensity=data[:, 2], fill="red", **kwargs)
with pytest.raises(GMTInvalidInput):
fig.plot3d(style="cc", fill="red", transparency=data[:, 2] * 100, **kwargs)
@pytest.mark.mpl_image_compare
def test_plot3d_projection(data, region):
"""
Plot the data in green squares with a projection.
"""
fig = Figure()
fig.plot3d(
x=data[:, 0],
y=data[:, 1],
z=data[:, 2],
zscale=5,
perspective=[225, 30],
region=region,
projection="R40/10c",
style="s1c",
fill="green",
frame=["ag", "zag"],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_colors(data, region):
"""
Plot the data using z as fills.
"""
fig = Figure()
fig.plot3d(
x=data[:, 0],
y=data[:, 1],
z=data[:, 2],
zscale=5,
perspective=[225, 30],
fill=data[:, 2],
region=region,
projection="X6c",
style="c0.5c",
cmap="cubhelix",
frame=["afg", "zafg"],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_sizes(data, region):
"""
Plot the data using z as sizes.
"""
fig = Figure()
fig.plot3d(
x=data[:, 0],
y=data[:, 1],
z=data[:, 2],
zscale=5,
perspective=[225, 30],
size=0.5 * data[:, 2],
region=region,
projection="X10c",
# Using inches instead of cm because of upstream bug at
# https://github.com/GenericMappingTools/gmt/issues/4386
style="ui",
fill="blue",
frame=["af", "zaf"],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_colors_sizes(data, region):
"""
Plot the data using z as sizes and fills.
"""
fig = Figure()
fig.plot3d(
x=data[:, 0],
y=data[:, 1],
z=data[:, 2],
zscale=5,
perspective=[225, 30],
fill=data[:, 2],
size=0.5 * data[:, 2],
region=region,
projection="X6c",
# Using inches instead of cm because of upstream bug at
# https://github.com/GenericMappingTools/gmt/issues/4386
style="ui",
cmap="copper",
frame=["af", "zaf"],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_colors_sizes_proj(data, region):
"""
Plot the data using z as sizes and fills with a projection.
"""
fig = Figure()
fig.plot3d(
x=data[:, 0],
y=data[:, 1],
z=data[:, 2],
zscale=5,
perspective=[225, 30],
region=region,
projection="M20c",
frame=["af", "zaf"],
fill=data[:, 2],
size=data[:, 2],
# Using inches instead of cm because of upstream bug at
# https://github.com/GenericMappingTools/gmt/issues/4386
style="ui",
cmap="copper",
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_varying_intensity():
"""
Plot the data with array-like intensity.
"""
x = np.arange(-1, 1.1, 0.1)
y = np.zeros(x.size)
z = y
intensity = x
fig = Figure()
fig.plot3d(
x=x,
y=y,
z=z,
region=[-1.1, 1.1, -0.5, 0.5, -0.5, 0.5],
projection="X15c/5c",
zsize="5c",
perspective=[135, 30],
frame=["Sltr", "xaf+lIntensity"],
style="c0.5c",
fill="blue",
intensity=intensity,
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_transparency():
"""
Plot the data with a constant transparency.
"""
x = np.arange(1, 10)
y = np.arange(1, 10)
z = np.arange(1, 10) * 10
fig = Figure()
fig.plot3d(
x=x,
y=y,
z=z,
style="u0.2c",
fill="blue",
region=[0, 10, 0, 10, 10, 90],
projection="X10c",
zscale=0.1,
frame=True,
perspective=[135, 30],
transparency=80.0,
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_varying_transparency():
"""
Plot the data using z as transparency using 3-D column symbols.
"""
x = np.arange(1, 10)
y = np.arange(1, 10)
z = np.arange(1, 10) * 10
fig = Figure()
fig.plot3d(
x=x,
y=y,
z=z,
style="o0.2c+B5",
fill="blue",
region=[0, 10, 0, 10, 10, 90],
projection="X10c",
zscale=0.1,
frame=True,
perspective=[135, 30],
transparency=z,
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_sizes_colors_transparencies():
"""
Plot the data with varying sizes and fills using z as transparency.
"""
x = np.arange(1.0, 10.0)
y = np.arange(1.0, 10.0)
z = np.arange(1, 10) * 10
fill = np.arange(1, 10) * 0.15
size = np.arange(1, 10) * 0.2
transparency = np.arange(1, 10) * 10
fig = Figure()
fig.plot3d(
x=x,
y=y,
z=z,
region=[0, 10, 0, 10, 10, 90],
projection="X10c",
zscale=0.1,
perspective=[135, 30],
frame=True,
style="uc",
fill=fill,
size=size,
cmap="gray",
transparency=transparency,
)
return fig
@pytest.mark.mpl_image_compare
@pytest.mark.mpl_image_compare(filename="test_plot3d_matrix.png")
@pytest.mark.parametrize("fill", ["#aaaaaa", 170])
def test_plot3d_matrix(data, region, fill):
"""
Plot the data passing in a matrix and specifying incols.
"""
fig = Figure()
fig.plot3d(
data,
zscale=5,
perspective=[225, 30],
region=region,
projection="M20c",
style="c1c",
fill=fill,
frame=["a", "za"],
incols="0,1,2",
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_matrix_color(data, region):
"""
Plot the data passing in a matrix and using a colormap.
"""
fig = Figure()
fig.plot3d(
data,
zscale=5,
perspective=[225, 30],
region=region,
projection="X10c",
style="c0.5c",
cmap="rainbow",
incols=[0, 1, 2, 2],
frame=["a", "za"],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_from_file(region):
"""
Plot using the data file name instead of loaded data.
"""
fig = Figure()
fig.plot3d(
data=POINTS_DATA,
zscale=5,
perspective=[225, 30],
region=region,
projection="X20c",
style="d1c",
fill="yellow",
frame=["af", "zaf"],
incols=[0, 1, 2],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_vectors():
"""
Plot vectors.
"""
azimuth = np.array([0, 45, 90, 135, 180, 225, 270, 310])
lengths = np.linspace(0.1, 1, len(azimuth))
lon = np.sin(np.deg2rad(azimuth))
lat = np.cos(np.deg2rad(azimuth))
elev = np.tan(np.deg2rad(azimuth))
fig = Figure()
fig.plot3d(
x=lon,
y=lat,
z=elev,
zscale=2,
perspective=[225, 30],
direction=(azimuth, lengths),
region=[-2, 2, -2, 2, -2, 2],
projection="X10c",
style="V1c+e+n",
fill="black",
frame=["af", "zaf"],
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_scalar_xyz():
"""
Plot symbols given scalar x, y, z coordinates.
"""
fig = Figure()
fig.basemap(
region=[-2, 2, -2, 2, -2, 2],
frame=["xaf+lx", "yaf+ly", "zaf+lz"],
zscale=2,
perspective=[225, 30],
)
fig.plot3d(
x=-1.5, y=1.5, z=-1.5, style="c1c", fill="red", zscale=True, perspective=True
)
fig.plot3d(x=0, y=0, z=0, style="t1c", fill="green", zscale=True, perspective=True)
fig.plot3d(
x=1.5, y=-1.5, z=1.5, style="s1c", fill="blue", zscale=True, perspective=True
)
return fig
@pytest.mark.mpl_image_compare(
filename="test_plot3d_ogrgmt_file_multipoint_default_style.png"
)
@pytest.mark.parametrize("func", [str, Path])
def test_plot3d_ogrgmt_file_multipoint_default_style(func):
"""
Make sure that OGR/GMT files with MultiPoint geometry are plotted as cubes
and not as line (default GMT style).
"""
with GMTTempFile(suffix=".gmt") as tmpfile:
gmt_file = """# @VGMT1.0 @GMULTIPOINT
# @R1/1.5/1/1.5
# FEATURE_DATA
>
1 1 2
1.5 1.5 1"""
with open(tmpfile.name, "w", encoding="utf8") as file:
file.write(gmt_file)
fig = Figure()
fig.plot3d(
data=func(tmpfile.name),
perspective=[315, 25],
region=[0, 2, 0, 2, 0, 2],
projection="X2c",
frame=["WsNeZ1", "xag", "yag", "zag"],
zscale=1.5,
)
return fig
@pytest.mark.mpl_image_compare
def test_plot3d_ogrgmt_file_multipoint_non_default_style():
"""
Make sure that non-default style can be set for plotting OGR/GMT file.
"""
with GMTTempFile(suffix=".gmt") as tmpfile:
gmt_file = """# @VGMT1.0 @GMULTIPOINT
# @R1/1.5/1/1.5
# FEATURE_DATA
>
1 1 2
1.5 1.5 1"""
with open(tmpfile.name, "w", encoding="utf8") as file:
file.write(gmt_file)
fig = Figure()
fig.plot3d(
data=tmpfile.name,
perspective=[315, 25],
region=[0, 2, 0, 2, 0, 2],
projection="X2c",
frame=["WsNeZ1", "xag", "yag", "zag"],
zscale=1.5,
style="c0.2c",
)
return fig