-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtest_format.py
More file actions
271 lines (232 loc) · 7.81 KB
/
Copy pathtest_format.py
File metadata and controls
271 lines (232 loc) · 7.81 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
#!/usr/bin/env python3
"""
Test format and rc behavior.
"""
import locale
import numpy as np
import proplot as pplt
state = np.random.RandomState(51423)
def test_colormap_assign():
"""
Test below line is possible and naming schemes.
"""
pplt.rc['image.cmap'] = pplt.Colormap('phase', shift=180, left=0.2)
assert pplt.rc['cmap'] == pplt.rc['cmap.sequential'] == '_Phase_copy_s'
pplt.rc['image.cmap'] = pplt.Colormap('magma', reverse=True, right=0.8)
assert pplt.rc['image.cmap'] == pplt.rc['cmap.sequential'] == '_magma_copy_r'
def test_ignored_keywords():
"""
Test ignored keywords and functions.
"""
with pplt.tests.warns(pplt.internals.ProplotWarning) as record:
fig, ax = pplt.subplots(
gridspec_kw={'left': 3},
subplot_kw={'proj': 'cart'},
subplotpars={'left': 0.2},
)
assert len(record) == 3
with pplt.tests.warns(pplt.internals.ProplotWarning) as record:
fig.subplots_adjust(left=0.2)
assert len(record) == 1
@pplt.tests.image_compare
def test_init_format():
"""
Test application of format args on initialization.
"""
fig, axs = pplt.subplots(
ncols=2, xlim=(0, 10), xlabel='xlabel',
abc=True, title='Subplot title',
collabels=['Column 1', 'Column 2'], suptitle='Figure title',
)
axs[0].format(hatch='xxx', hatchcolor='k', facecolor='blue3')
@pplt.tests.image_compare
def test_patch_format():
"""
Test application of patch args on initialization.
"""
fig = pplt.figure(suptitle='Super title')
fig.subplot(
121, proj='cyl', labels=True, land=True, latlines=20,
abcloc='l', abc='[A]'
)
fig.subplot(
122, facecolor='gray1', color='red', titleloc='l', title='Hello',
abcloc='l', abc='[A]', xticks=0.1, yformatter='scalar'
)
@pplt.tests.image_compare
def test_multi_formatting():
"""
Support formatting in multiple projections.
"""
fig, axs = pplt.subplots(ncols=2, proj=('cart', 'cyl'))
axs[0].pcolormesh(state.rand(5, 5))
fig.format(
land=1, labels=1, lonlim=(0, 90), latlim=(0, 90), xlim=(0, 10), ylim=(0, 10),
)
axs[:1].format(
land=1, labels=1, lonlim=(0, 90), latlim=(0, 90), xlim=(0, 10), ylim=(0, 10),
)
@pplt.tests.image_compare
def test_inner_title_zorder():
"""
Test prominence of contour labels and whatnot.
"""
fig, ax = pplt.subplots()
ax.format(
title='TITLE', titleloc='upper center', titleweight='bold', titlesize='xx-large'
)
ax.format(xlim=(0, 1), ylim=(0, 1))
ax.text(
0.5, 0.95, 'text', ha='center', va='top', color='red',
weight='bold', size='xx-large'
)
x = [[0.4, 0.6]] * 2
y = z = [[0.9, 0.9], [1.0, 1.0]]
ax.contour(
x, y, z, color='k', labels=True,
levels=None, labels_kw={'color': 'blue', 'weight': 'bold', 'size': 'xx-large'}
)
@pplt.tests.image_compare
def test_font_adjustments():
"""
Test font name application. Somewhat hard to do.
"""
fig, axs = pplt.subplots(ncols=2)
axs.format(
abc='A.',
fontsize=15,
fontname='Fira Math',
xlabel='xlabel',
ylabel='ylabel',
title='Title',
figtitle='Figure title',
collabels=['Column 1', 'Column 2'],
)
@pplt.tests.image_compare
def test_axes_colors():
"""
Test behavior of passing color to format.
"""
fig, axs = pplt.subplots(
ncols=3, nrows=2, share=False,
proj=('cyl', 'cart', 'polar', 'cyl', 'cart', 'polar'), wratios=(2, 2, 1)
)
axs[:, 0].format(labels=True)
axs[:3].format(edgecolor='red', gridlabelsize='med-large', gridlabelweight='bold')
axs[:3].format(color='red') # without this just colors the edge
axs[1].format(xticklabelcolor='gray')
# axs[2].format(ticklabelcolor='red')
axs[1].format(tickcolor='blue')
axs[3:].format(color='red') # ensure propagates
# axs[-1].format(gridlabelcolor='green') # should work
@pplt.tests.image_compare
def test_locale_formatting():
"""
Ensure locale formatting works. Also zerotrim should account
for non-period decimal separators.
"""
locale.setlocale(locale.LC_ALL, 'de_DE')
pplt.rc['formatter.use_locale'] = False
pplt.rc['formatter.zerotrim'] = True
with pplt.rc.context({'formatter.use_locale': True}):
fig, ax = pplt.subplots()
ticks = pplt.arange(-1, 1, 0.1)
ax.format(ylim=(min(ticks), max(ticks)), yticks=ticks)
@pplt.tests.image_compare
def test_bounds_ticks():
"""
Test spine bounds and location. Previously applied `fixticks`
automatically but no longer the case.
"""
fig, ax = pplt.subplots()
# ax.format(xlim=(-10, 10))
ax.format(xloc='top')
ax.format(xlim=(-10, 15), xbounds=(0, 10))
@pplt.tests.image_compare
def test_cutoff_ticks():
"""
Test spine cutoff ticks.
"""
fig, ax = pplt.subplots()
# ax.format(xlim=(-10, 10))
ax.format(xlim=(-10, 10), xscale=('cutoff', 0, 2), xloc='top', fixticks=True)
ax.axvspan(0, 100, facecolor='k', alpha=0.1)
@pplt.tests.image_compare
def test_spine_side():
"""
Test automatic spine selection when passing `xspineloc` or `yspineloc`.
"""
fig, ax = pplt.subplots()
ax.plot(pplt.arange(-5, 5), (10 * state.rand(11, 5) - 5).cumsum(axis=0))
ax.format(xloc='bottom', yloc='zero')
ax.alty(loc='right')
@pplt.tests.image_compare
def test_spine_offset():
"""
Test offset axes.
"""
fig, ax = pplt.subplots()
ax.format(xloc='none') # test none instead of neither
ax.alty(loc=('axes', -0.2), color='red')
# ax.alty(loc=('axes', 1.2), color='blue')
ax.alty(loc=('axes', -0.4), color='blue')
ax.alty(loc=('axes', 1.1), color='green')
@pplt.tests.image_compare
def test_tick_direction():
"""
Test tick direction arguments.
"""
fig, axs = pplt.subplots(ncols=2)
axs[0].format(tickdir='in')
axs[1].format(xtickdirection='inout', ytickdir='out') # rc setting should be used?
@pplt.tests.image_compare
def test_tick_length():
"""
Test tick length args. Ensure ratios can be applied successively.
"""
fig, ax = pplt.subplots()
ax.format(yticklen=100)
ax.format(xticklen=50, yticklenratio=0.1)
@pplt.tests.image_compare
def test_tick_width():
"""
Test tick width args. Ensure ratios can be applied successively, setting
width to `zero` adjusts length for label padding, and ticks can appear
without spines if requested.
"""
fig, axs = pplt.subplots(ncols=2, nrows=2, share=False)
ax = axs[0]
ax.format(linewidth=2, ticklen=20, xtickwidthratio=1)
ax.format(ytickwidthratio=0.3)
ax = axs[1]
ax.format(axeslinewidth=0, ticklen=20, tickwidth=2) # should permit ticks
ax = axs[2]
ax.format(tickwidth=0, ticklen=50) # should set length to zero
ax = axs[3]
ax.format(linewidth=0, ticklen=20, tickwidth='5em') # should override linewidth
@pplt.tests.image_compare
def test_tick_labels():
"""
Test default and overwriting properties of auto tick labels.
"""
import pandas as pd
data = state.rand(5, 3)
data = pd.DataFrame(data, index=['foo', 'bar', 'baz', 'bat', 'bot'])
fig, axs = pplt.subplots(abc='A.', abcloc='ul', ncols=2, refwidth=3, span=False)
for i, ax in enumerate(axs):
data.index.name = 'label'
if i == 1:
ax.format(xformatter='null') # overrides result
ax.bar(data, autoformat=True)
if i == 0:
data.index = ['abc', 'def', 'ghi', 'jkl', 'mno']
data.index.name = 'foobar' # label should be updated
ax.bar(-data, autoformat=True)
@pplt.tests.image_compare
def test_label_settings():
"""
Test label colors and ensure color change does not erase labels.
"""
fig, ax = pplt.subplots()
ax.format(xlabel='xlabel', ylabel='ylabel')
ax.format(labelcolor='red')