Skip to content

Commit 890f501

Browse files
committed
Partial commit
1 parent 8d68d59 commit 890f501

12 files changed

+1793
-11
lines changed

Diff for: proplot/tests/test_1dplots.py

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

0 commit comments

Comments
 (0)