Skip to content

Commit 79a1ae2

Browse files
committed
Add initial tests from test notebook
1 parent 8d68d59 commit 79a1ae2

13 files changed

+1802
-11
lines changed

Diff for: proplot/tests/test_1dplots.py

+307
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test 1D plotting overrides.
4+
"""
5+
import numpy as np
6+
import numpy.ma as ma
7+
import pandas as pd
8+
9+
import proplot as pplt
10+
11+
# ## Edge fix
12+
#
13+
# Test edge fix applied to 1D plotting utilities.
14+
15+
# %matplotlib inline
16+
17+
pplt.rc.inlinefmt = 'svg'
18+
pplt.rc.edgefix = 1
19+
fig, axs = pplt.subplots(ncols=2, share=False)
20+
axs.format(grid=False)
21+
b = axs[0].bar(np.random.rand(10,) * 10 - 5, width=1, negpos=True)
22+
axs[1].area(np.random.rand(5, 3), stack=True)
23+
24+
#
25+
# Make sure `edgefix` is not applied wherever transparent colorbars appear.
26+
27+
pplt.rc.inlinefmt = 'svg'
28+
data = np.random.rand(10, 10)
29+
cmap = 'magma'
30+
fig, axs = pplt.subplots(nrows=3, ncols=2, refwidth=2.5, share=False)
31+
for i, iaxs in enumerate((axs[:2], axs[2:4])):
32+
if i == 0:
33+
cmap = pplt.Colormap('magma', alpha=0.5)
34+
alpha = None
35+
iaxs.format(title='Colormap alpha')
36+
else:
37+
cmap = 'magma'
38+
alpha = 0.5
39+
iaxs.format(title='Single alpha')
40+
iaxs[0].contourf(data, cmap=cmap, colorbar='b', alpha=alpha)
41+
iaxs[1].pcolormesh(data, cmap=cmap, colorbar='b', alpha=alpha)
42+
axs[4].bar(data[:3, :3], alpha=0.5)
43+
axs[5].area(data[:3, :3], alpha=0.5, stack=True)
44+
45+
# %% [markdown]
46+
# ## Auto reverse
47+
#
48+
# Test enabled and disabled auto reverse.
49+
50+
# %%
51+
x = np.arange(10)[::-1]
52+
y = np.arange(10)
53+
z = np.random.rand(10, 10)
54+
fig, axs = pplt.subplots(ncols=2, nrows=2, share=False)
55+
# axs[0].format(xreverse=False) # should fail
56+
axs[0].plot(x, y)
57+
axs[1].format(xlim=(0, 9)) # manual override
58+
axs[1].plot(x, y)
59+
axs[2].pcolor(x, y[::-1], z)
60+
axs[3].format(xlim=(0, 9), ylim=(0, 9)) # manual override!
61+
axs[3].pcolor(x, y[::-1], z)
62+
63+
64+
# ## Invalid values
65+
#
66+
# Test distributions with missing or invalid values.
67+
68+
fig, axs = pplt.subplots(ncols=2)
69+
data = np.random.normal(size=(100, 5))
70+
for j in range(5):
71+
data[:, j] = np.sort(data[:, j])
72+
data[:19 * (j + 1), j] = np.nan
73+
# data[:20, :] = np.nan
74+
data_masked = ma.masked_invalid(data) # should be same result
75+
for ax, dat in zip(axs, (data, data_masked)):
76+
ax.plot(dat, means=True, shade=True)
77+
78+
fig, axs = pplt.subplots(ncols=2, nrows=2)
79+
data = np.random.normal(size=(100, 5))
80+
for i in range(5): # test uneven numbers of invalid values
81+
data[:10 * (i + 1), :] = np.nan
82+
data_masked = ma.masked_invalid(data) # should be same result
83+
for ax, dat in zip(axs[:2], (data, data_masked)):
84+
ax.violin(dat, means=True)
85+
for ax, dat in zip(axs[2:], (data, data_masked)):
86+
ax.box(dat, fill=True, means=True)
87+
88+
# ## Histogram types
89+
#
90+
# Test the different histogram types using basic keywords.
91+
92+
fig, axs = pplt.subplots(ncols=2, nrows=2, share=False)
93+
data = np.random.normal(size=(100, 5))
94+
data += np.arange(5)
95+
kws = ({'stack': False}, {'stack': True}, {'fill': False}, {'fill': True, 'alpha': 0.5})
96+
for ax, kw in zip(axs, kws):
97+
ax.hist(data, ec='k', **kw)
98+
99+
100+
# ## Scatter inbounds
101+
#
102+
# Test in-bounds scatter plots.
103+
104+
fig, axs = pplt.subplots(ncols=2, share=False)
105+
N = 100
106+
fig.format(xlim=(0, 20))
107+
for i, ax in enumerate(axs):
108+
c = ax.scatter(np.arange(N), np.arange(N), c=np.arange(N), inbounds=i)
109+
ax.colorbar(c, loc='b')
110+
111+
# ## Scatter columns
112+
#
113+
# Test scatter column iteration and property cycling. Note we cannot
114+
# retrieve metadata from `s` and `c`.
115+
#
116+
117+
fig, ax = pplt.subplots()
118+
cycle = pplt.Cycle(
119+
'538', marker=['X', 'o', 's', 'd'], sizes=[50, 100], edgecolors=['r', 'k']
120+
)
121+
ax.scatter(np.random.rand(10, 4), np.random.rand(10, 4), cycle=cycle)
122+
123+
fig, axs = pplt.subplots(ncols=2)
124+
axs[0].plot(np.random.rand(5, 5), np.random.rand(5, 5), lw=5)
125+
axs[1].scatter(np.random.rand(5, 5), np.random.rand(5, 5), s=np.random.rand(5, 5) * 300)
126+
127+
# ## Scatter colors
128+
129+
# Test diverse scatter keyword parsing.
130+
131+
132+
x = np.random.randn(60)
133+
y = np.random.randn(60)
134+
135+
fig, axs = pplt.subplots()
136+
axs.scatter(x, y, s=80, fc='none', edgecolors='r')
137+
138+
# Test RGB color scaling.
139+
140+
fig, axs = pplt.subplots(ncols=3)
141+
data = np.random.rand(50, 3)
142+
ax = axs[0]
143+
ax.scatter(data, c=data, cmap='reds')
144+
ax = axs[1]
145+
ax.scatter(data[:, 0], c=data, cmap='reds', ) # cycle='foo') # should warn
146+
ax = axs[2]
147+
ax.scatter(data, mean=True, shadestd=1, barstd=0.5)
148+
ax.format(xlim=(-0.1, 2.1))
149+
150+
# ## Scatter sizes
151+
152+
# Test marker size scaling.
153+
154+
fig = pplt.figure()
155+
ax = fig.subplot(margin=0.15)
156+
data = np.random.rand(5) * 500
157+
ax.scatter(
158+
np.arange(5),
159+
[0.25] * 5,
160+
c='blue7',
161+
sizes=['5pt', '10pt', '15pt', '20pt', '25pt']
162+
)
163+
ax.scatter(
164+
np.arange(5),
165+
[0.50] * 5,
166+
c='red7',
167+
sizes=data,
168+
absolute_size=True
169+
)
170+
ax.scatter(
171+
np.arange(5),
172+
[0.75] * 5,
173+
c='red7',
174+
sizes=data,
175+
absolute_size=True
176+
)
177+
for i, d in enumerate(data):
178+
ax.text(i, 0.5, format(d, '.0f'), va='center', ha='center')
179+
180+
fig, axs = pplt.subplots(ncols=3)
181+
pplt.rc.reset()
182+
pplt.rc['lines.markersize'] = 20
183+
N = 50
184+
x = np.random.rand(N)
185+
y = np.random.rand(N)
186+
for i, ax in enumerate(axs):
187+
kw = {'absolute_size': i == 2}
188+
if i == 1:
189+
kw['smax'] = 20 ** 2 # should be same as relying on lines.markersize
190+
ax.scatter(x, y, x * y, **kw)
191+
192+
# ## Relative bar width
193+
194+
# Test relative and absolute widths.
195+
196+
fig, axs = pplt.subplots(ncols=3)
197+
x = np.arange(10)
198+
y = np.random.rand(10, 2)
199+
for i, ax in enumerate(axs):
200+
ax.bar(x * (2 * i + 1), y, width=0.8, absolute_width=i == 1)
201+
202+
# ## Pie charts
203+
#
204+
# Test basic pie plots. No examples in user guide right now.
205+
206+
pplt.rc.inlinefmt = 'svg'
207+
labels = ['foo', 'bar', 'baz', 'biff', 'buzz']
208+
array = np.arange(1, 6)
209+
data = pd.Series(array, index=labels)
210+
fig = pplt.figure()
211+
ax = fig.subplot(121)
212+
ax.pie(array, edgefix=True, labels=labels, ec='k', cycle='reds')
213+
ax = fig.subplot(122)
214+
ax.pie(data, ec='k', cycle='blues')
215+
216+
# ## Box violin plots
217+
#
218+
# Test new default behavior of passing cycles to box/violin commands.
219+
220+
fig = pplt.figure()
221+
ax = fig.subplot(121)
222+
ax.box(
223+
np.random.uniform(-3, 3, size=(1000, 5)),
224+
# cycle='blues_r',
225+
fillcolor=['red', 'blue', 'green', 'orange', 'yellow'],
226+
# ec='face',
227+
)
228+
ax = fig.subplot(122)
229+
ax.violin(
230+
np.random.normal(0, 1, size=(1000, 5)),
231+
# cycle='greys',
232+
fillcolor=['gray1', 'gray7'],
233+
means=True,
234+
barstds=2,
235+
)
236+
237+
238+
# Sample data
239+
N = 500
240+
state = np.random.RandomState(51423)
241+
data1 = state.normal(size=(N, 5)) + 2 * (state.rand(N, 5) - 0.5) * np.arange(5)
242+
data1 = pd.DataFrame(data1, columns=pd.Index(list('abcde'), name='label'))
243+
data2 = state.rand(100, 7)
244+
data2 = pd.DataFrame(data2, columns=pd.Index(list('abcdefg'), name='label'))
245+
246+
# Figure
247+
fig, axs = pplt.subplots([[1, 1, 2, 2], [0, 3, 3, 0]], span=False)
248+
axs.format(
249+
abc='A.', titleloc='l', grid=False,
250+
suptitle='Boxes and violins demo'
251+
)
252+
253+
# Box plots
254+
ax = axs[0]
255+
obj1 = ax.box(data1, means=True, marker='x', meancolor='r', fillcolor='gray4')
256+
print(obj1)
257+
ax.format(title='Box plots')
258+
259+
# Violin plots
260+
ax = axs[1]
261+
obj2 = ax.violin(data1, fillcolor='gray6', means=True, points=100)
262+
print(obj2)
263+
ax.format(title='Violin plots')
264+
265+
# Boxes with different colors
266+
ax = axs[2]
267+
ax.format(title='Multiple colors', ymargin=0.15)
268+
ax.boxh(data2, cycle='pastel2')
269+
270+
# ## Parametric labels
271+
#
272+
# Test passing strings as parametric 'color values'. Likely a common use case.
273+
274+
pplt.rc.inlinefmt = 'svg'
275+
fig, ax = pplt.subplots()
276+
c = ax.parametric(
277+
np.random.rand(5), c=list('abcde'), lw=20, colorbar='b', cmap_kw={'left': 0.2}
278+
)
279+
280+
# ## Parametric labels
281+
#
282+
# Test parametric plots with string labels.
283+
284+
fig, ax = pplt.subplots()
285+
ax.parametric(
286+
np.random.rand(5), c=list('abcde'), lw=5, colorbar='b', cmap_kw={'left': 0.2}
287+
)
288+
289+
# ## Parametric color input
290+
#
291+
# Test color input arguments. Should be able to make monochromatic
292+
# plots for case where we want `line` without sticky x/y edges.
293+
294+
# %matplotlib inline
295+
fig, axs = pplt.subplots(ncols=2, nrows=2)
296+
colors = (
297+
[(0, 1, 1), (0, 1, 0), (1, 0, 0), (0, 0, 1), (1, 1, 0)],
298+
['b', 'r', 'g', 'm', 'c', 'y'],
299+
'black',
300+
(0.5, 0.5, 0.5),
301+
# [(0.5, 0.5, 0.5)],
302+
)
303+
for ax, color in zip(axs, colors):
304+
ax.parametric(
305+
np.random.rand(5), np.random.rand(5),
306+
linewidth=2, label='label', color=color, colorbar='b', legend='b'
307+
)

0 commit comments

Comments
 (0)