-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtest_2dplots.py
More file actions
322 lines (282 loc) · 9.48 KB
/
Copy pathtest_2dplots.py
File metadata and controls
322 lines (282 loc) · 9.48 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
#!/usr/bin/env python3
"""
Test 2D plotting overrides.
"""
import numpy as np
import pytest
import xarray as xr
import proplot as pplt
state = np.random.RandomState(51423)
@pplt.tests.image_compare
def test_colormap_vcenter():
"""
Test colormap vcenter.
"""
fig, axs = pplt.subplots(ncols=3)
data = 10 * state.rand(10, 10) - 3
axs[0].pcolor(data, vcenter=0)
axs[1].pcolor(data, vcenter=1)
axs[2].pcolor(data, vcenter=2)
@pplt.tests.image_compare
def test_auto_diverging():
"""
Test that auto diverging works.
"""
# Test with basic data
fig = pplt.figure()
# fig.format(collabels=('Auto sequential', 'Auto diverging'), suptitle='Default')
ax = fig.subplot(121)
ax.pcolor(state.rand(10, 10) * 5, colorbar='b')
ax = fig.subplot(122)
ax.pcolor(state.rand(10, 10) * 5 - 3.5, colorbar='b')
fig.format(toplabels=('Sequential', 'Diverging'))
# Test with explicit vcenter
fig, axs = pplt.subplots(ncols=3)
data = 5 * state.rand(10, 10)
axs[0].pcolor(data, vcenter=0, colorbar='b') # otherwise should be disabled
axs[1].pcolor(data, vcenter=1.5, colorbar='b')
axs[2].pcolor(data, vcenter=4, colorbar='b', symmetric=True)
# Test when cmap input disables auto diverging.
fig, axs = pplt.subplots(ncols=2, nrows=2, refwidth=2)
cmap = pplt.Colormap(('red7', 'red3', 'red1', 'blue1', 'blue3', 'blue7'), listmode='discrete') # noqa: E501
data1 = 10 * state.rand(10, 10)
data2 = data1 - 2
for i, cmap in enumerate(('RdBu_r', cmap)):
for j, data in enumerate((data1, data2)):
cmap = pplt.Colormap(pplt.Colormap(cmap))
axs[i, j].pcolormesh(data, cmap=cmap, colorbar='b')
fig, axs = pplt.subplots(ncols=3)
data = state.rand(5, 5) * 10 - 5
for i, ax in enumerate(axs[:2]):
ax.pcolor(data, sequential=bool(i), colorbar='b')
axs[2].pcolor(data, diverging=False, colorbar='b') # should have same effect
fig, axs = pplt.subplots(ncols=2)
data = state.rand(5, 5) * 10 + 2
for ax, norm in zip(axs, (None, 'div')):
ax.pcolor(data, norm=norm, colorbar='b')
@pplt.tests.image_compare
def test_colormap_mode():
"""
Test auto extending, auto discrete. Should issue warnings.
"""
fig, axs = pplt.subplots(ncols=2, nrows=2, share=False)
axs[0].pcolor(state.rand(5, 5) % 0.3, extend='both', cyclic=True, colorbar='b')
axs[1].pcolor(state.rand(5, 5), sequential=True, diverging=True, colorbar='b')
axs[2].pcolor(state.rand(5, 5), discrete=False, qualitative=True, colorbar='b')
pplt.rc['cmap.discrete'] = False # should be ignored below
axs[3].contourf(state.rand(5, 5), colorbar='b')
@pplt.tests.image_compare
def test_contour_labels():
"""
Test contour labels. We use a separate `contour` object when adding labels to
filled contours or else weird stuff happens (see below). We could just modify
filled contour edges when not adding labels but that would be inconsistent with
behavior when labels are active.
"""
data = state.rand(5, 5) * 10 - 5
fig, axs = pplt.subplots(ncols=2)
ax = axs[0]
ax.contourf(
data, edgecolor='k', linewidth=1.5,
labels=True, labels_kw={'color': 'k', 'size': 'large'}
)
ax = axs[1]
m = ax.contourf(data)
ax.clabel(m, colors='black', fontsize='large') # looks fine without this
for o in m.collections:
o.set_linewidth(1.5)
o.set_edgecolor('k')
@pplt.tests.image_compare
def test_contour_negative():
"""
Ensure `cmap.monochrome` properly assigned.
"""
fig = pplt.figure(share=False)
ax = fig.subplot(131)
data = state.rand(10, 10) * 10 - 5
ax.contour(data, color='k')
ax = fig.subplot(132)
ax.tricontour(*(state.rand(3, 20) * 10 - 5), color='k')
ax = fig.subplot(133)
ax.contour(data, cmap=['black']) # fails but that's ok
@pplt.tests.image_compare
def test_contour_single():
"""
Test whether single contour works.
"""
da = xr.DataArray(
np.array(
[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]
]
),
dims=['y', 'x']
)
fig, ax = pplt.subplots()
ax.contour(da, levels=[5.0], color='r')
@pplt.tests.image_compare
def test_edge_fix():
"""
Test edge fix applied to 1D plotting utilities.
"""
# Test basic application
# TODO: This should make no difference for PNG plots?
pplt.rc.edgefix = 1
fig, axs = pplt.subplots(ncols=2, share=False)
axs.format(grid=False)
axs[0].bar(state.rand(10,) * 10 - 5, width=1, negpos=True)
axs[1].area(state.rand(5, 3), stack=True)
# Test whether ignored for transparent colorbars
data = state.rand(10, 10)
cmap = 'magma'
fig, axs = pplt.subplots(nrows=3, ncols=2, refwidth=2.5, share=False)
for i, iaxs in enumerate((axs[:2], axs[2:4])):
if i == 0:
cmap = pplt.Colormap('magma', alpha=0.5)
alpha = None
iaxs.format(title='Colormap alpha')
else:
cmap = 'magma'
alpha = 0.5
iaxs.format(title='Single alpha')
iaxs[0].contourf(data, cmap=cmap, colorbar='b', alpha=alpha)
iaxs[1].pcolormesh(data, cmap=cmap, colorbar='b', alpha=alpha)
axs[4].bar(data[:3, :3], alpha=0.5)
axs[5].area(data[:3, :3], alpha=0.5, stack=True)
@pplt.tests.image_compare
def test_flow_functions():
"""
These are seldom used and missing from documentation. Be careful
not to break anything basic.
"""
fig, ax = pplt.subplots()
for _ in range(2):
ax.streamplot(state.rand(10, 10), 5 * state.rand(10, 10), label='label')
fig, axs = pplt.subplots(ncols=2)
ax = axs[0]
ax.quiver(
state.rand(10, 10), 5 * state.rand(10, 10), c=state.rand(10, 10),
label='label'
)
ax = axs[1]
ax.quiver(state.rand(10), state.rand(10), label='single')
@pplt.tests.image_compare
def test_gray_adjustment():
"""
Test gray adjustments when creating segmented colormaps.
"""
fig, ax = pplt.subplots()
data = state.rand(5, 5) * 10 - 5
cmap = pplt.Colormap(['blue', 'grey3', 'red'])
ax.pcolor(data, cmap=cmap, colorbar='b')
@pplt.tests.image_compare
def test_ignore_message():
"""
Test various ignored argument warnings.
"""
warning = pplt.internals.ProplotWarning
fig, axs = pplt.subplots(ncols=2, nrows=2)
with pytest.warns(warning):
axs[0].contour(
state.rand(5, 5) * 10, levels=pplt.arange(10), symmetric=True
)
with pytest.warns(warning):
axs[1].contourf(
state.rand(10, 10), levels=np.linspace(0, 1, 10),
locator=5, locator_kw={}
)
with pytest.warns(warning):
axs[2].contourf(
state.rand(10, 10),
levels=pplt.arange(0, 1, 0.2),
vmin=0,
vmax=2,
locator=3,
colorbar='b'
)
with pytest.warns(warning):
axs[3].hexbin(
state.rand(1000),
state.rand(1000),
levels=pplt.arange(0, 20),
gridsize=10,
locator=2,
colorbar='b',
cmap='blues',
)
@pplt.tests.image_compare
def test_levels_with_vmin_vmax():
"""
Make sure `vmin` and `vmax` go into level generation algorithm.
"""
# Sample data
state = np.random.RandomState(51423)
x = y = np.array([-10, -5, 0, 5, 10])
data = state.rand(y.size, x.size)
# Figure
fig = pplt.figure(refwidth=2.3, share=False)
axs = fig.subplots()
m = axs.pcolormesh(x, y, data, vmax=1.35123)
axs.colorbar([m], loc='r')
@pplt.tests.image_compare
def test_level_restriction():
"""
Test `negative`, `positive`, and `symmetric` with and without discrete.
"""
fig, axs = pplt.subplots(ncols=3, nrows=2)
data = 20 * state.rand(10, 10) - 5
keys = ('negative', 'positive', 'symmetric')
for i, grp in enumerate((axs[:3], axs[3:])):
for j, ax in enumerate(grp):
kw = {keys[j]: True, 'discrete': bool(1 - i)}
ax.pcolor(data, **kw, colorbar='b')
@pplt.tests.image_compare
def test_qualitative_colormaps():
"""
Test both `colors` and `cmap` input and ensure extend setting is used for
extreme only if unset.
"""
fig, axs = pplt.subplots(ncols=2)
data = state.rand(5, 5)
colors = pplt.get_colors('set3')
for ax, extend in zip(axs, ('both', 'neither')):
ax.pcolor(
data, extend=extend,
colors=colors, colorbar='b'
)
fig, axs = pplt.subplots(ncols=2)
data = state.rand(5, 5)
cmap = pplt.Colormap('set3')
cmap.set_under('black') # does not overwrite
for ax, extend in zip(axs, ('both', 'neither')):
ax.pcolor(
data, extend=extend, cmap=cmap, colorbar='b'
)
@pplt.tests.image_compare
def test_segmented_norm():
"""
Test segmented norm with non-discrete levels.
"""
fig, ax = pplt.subplots()
ax.pcolor(
state.rand(5, 5) * 10,
discrete=False,
norm='segmented',
norm_kw={'levels': [0, 2, 10]},
colorbar='b'
)
@pplt.tests.image_compare
def test_triangular_functions():
"""
Test triangular functions. Here there is no remotely sensible way to infer
coordinates so we skip standardize function.
"""
fig, ax = pplt.subplots()
N = 30
y = state.rand(N) * 20
x = state.rand(N) * 50
da = xr.DataArray(state.rand(N), dims=('x',), coords={'x': x, 'y': ('x', y)})
ax.tricontour(da.x, da.y, da, labels=True)