Skip to content

Commit 9d1f659

Browse files
committed
Added a variety of tests
1 parent 60ec01b commit 9d1f659

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

tests/baseline/test_inset_basic.png

38.4 KB
Loading

tests/test_geographic.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import proplot as plt, numpy as np
2+
import pytest
3+
4+
5+
@pytest.mark.skip(reason="TODO")
6+
@pytest.mark.mpl_image_compare
7+
def test_geographic_single_projection():
8+
fig = plt.figure(refwidth=3)
9+
axs = fig.subplots(nrows=2, proj="robin", proj_kw={"lon_0": 180})
10+
# proj = pplt.Proj('robin', lon_0=180)
11+
# axs = pplt.subplots(nrows=2, proj=proj) # equivalent to above
12+
axs.format(
13+
suptitle="Figure with single projection",
14+
land=True,
15+
latlines=30,
16+
lonlines=60,
17+
)
18+
return fig
19+
20+
21+
@pytest.mark.skip(reason="TODO")
22+
@pytest.mark.mpl_image_compare
23+
def test_gegraphic_multiple_projections():
24+
fig = plt.figure()
25+
# Add projections
26+
gs = plt.GridSpec(ncols=2, nrows=3, hratios=(1, 1, 1.4))
27+
for i, proj in enumerate(("cyl", "hammer", "npstere")):
28+
ax1 = fig.subplot(gs[i, 0], proj=proj, basemap=True) # basemap
29+
ax2 = fig.subplot(gs[i, 1], proj=proj) # cartopy
30+
31+
# Format projections
32+
fig.format(
33+
land=True,
34+
suptitle="Figure with several projections",
35+
toplabels=("Basemap projections", "Cartopy projections"),
36+
toplabelweight="normal",
37+
latlines=30,
38+
lonlines=60,
39+
lonlabels="b",
40+
latlabels="r", # or lonlabels=True, labels=True, etc.
41+
)
42+
fig.subplotgrid[-2:].format(
43+
latlines=20, lonlines=30
44+
) # dense gridlines for polar plots
45+
plt.rc.reset()
46+
return fig
47+
48+
49+
@pytest.mark.skip(reason="TODO")
50+
@pytest.mark.mpl_image_compare
51+
def test_drawing_in_projection():
52+
# Fake data with unusual longitude seam location and without coverage over poles
53+
offset = -40
54+
lon = plt.arange(offset, 360 + offset - 1, 60)
55+
lat = plt.arange(-60, 60 + 1, 30)
56+
state = np.random.RandomState(51423)
57+
data = state.rand(len(lat), len(lon))
58+
59+
# Plot data both without and with globe=True
60+
for globe in (False, True):
61+
string = "with" if globe else "without"
62+
gs = plt.GridSpec(nrows=2, ncols=2)
63+
fig = plt.figure(refwidth=2.5)
64+
for i, ss in enumerate(gs):
65+
ax = fig.subplot(ss, proj="kav7", basemap=(i % 2))
66+
cmap = ("sunset", "sunrise")[i % 2]
67+
if i > 1:
68+
ax.pcolor(lon, lat, data, cmap=cmap, globe=globe, extend="both")
69+
else:
70+
m = ax.contourf(lon, lat, data, cmap=cmap, globe=globe, extend="both")
71+
fig.colorbar(m, loc="b", span=i + 1, label="values", extendsize="1.7em")
72+
fig.format(
73+
suptitle=f"Geophysical data {string} global coverage",
74+
toplabels=("Cartopy example", "Basemap example"),
75+
leftlabels=("Filled contours", "Grid boxes"),
76+
toplabelweight="normal",
77+
leftlabelweight="normal",
78+
coast=True,
79+
lonlines=90,
80+
abc="A.",
81+
abcloc="ul",
82+
abcborder=False,
83+
)
84+
return fig

tests/test_inset.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import proplot as pplt, numpy as np, pytest
2+
3+
4+
@pytest.mark.mpl_image_compare
5+
def test_inset_basic():
6+
# Demonstrate that complex arrangements preserve
7+
# spacing, aspect ratios, and axis sharing
8+
gs = pplt.GridSpec(nrows=2, ncols=2)
9+
fig = pplt.figure(refwidth=1.5, share=False)
10+
for ss, side in zip(gs, "tlbr"):
11+
ax = fig.add_subplot(ss)
12+
px = ax.panel_axes(side, width="3em")
13+
fig.format(
14+
xlim=(0, 1),
15+
ylim=(0, 1),
16+
xlabel="xlabel",
17+
ylabel="ylabel",
18+
xticks=0.2,
19+
yticks=0.2,
20+
title="Title",
21+
suptitle="Complex arrangement of panels",
22+
toplabels=("Column 1", "Column 2"),
23+
abc=True,
24+
abcloc="ul",
25+
titleloc="uc",
26+
titleabove=False,
27+
)
28+
return fig

tests/test_statistical_plotting.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
# import proplot as pplt
3+
import numpy as np, pandas as pd, proplot as pplt
4+
import pytest
5+
6+
7+
@pytest.mark.mpl_image_compare
8+
def test_statistical_boxplot():
9+
# Sample data
10+
N = 500
11+
state = np.random.RandomState(51423)
12+
data1 = state.normal(size=(N, 5)) + 2 * (state.rand(N, 5) - 0.5) * np.arange(5)
13+
data1 = pd.DataFrame(data1, columns=pd.Index(list("abcde"), name="label"))
14+
data2 = state.rand(100, 7)
15+
data2 = pd.DataFrame(data2, columns=pd.Index(list("abcdefg"), name="label"))
16+
17+
# Figure
18+
fig, axs = pplt.subplots([[1, 1, 2, 2], [0, 3, 3, 0]], span=False)
19+
axs.format(abc="A.", titleloc="l", grid=False, suptitle="Boxes and violins demo")
20+
21+
# Box plots
22+
ax = axs[0]
23+
obj1 = ax.box(data1, means=True, marker="x", meancolor="r", fillcolor="gray4")
24+
ax.format(title="Box plots")
25+
26+
# Violin plots
27+
ax = axs[1]
28+
obj2 = ax.violin(data1, fillcolor="gray6", means=True, points=100)
29+
ax.format(title="Violin plots")
30+
31+
# Boxes with different colors
32+
ax = axs[2]
33+
ax.boxh(data2, cycle="pastel2")
34+
ax.format(title="Multiple colors", ymargin=0.15)
35+
return fig
36+
37+
38+
@pytest.mark.mpl_image_compare
39+
def test_panel_dist():
40+
# Sample data
41+
N = 500
42+
state = np.random.RandomState(51423)
43+
x = state.normal(size=(N,))
44+
y = state.normal(size=(N,))
45+
bins = pplt.arange(-3, 3, 0.25)
46+
47+
# Histogram with marginal distributions
48+
fig, axs = pplt.subplots(ncols=2, refwidth=2.3)
49+
axs.format(
50+
abc="A.",
51+
abcloc="l",
52+
titleabove=True,
53+
ylabel="y axis",
54+
suptitle="Histograms with marginal distributions",
55+
)
56+
colors = ("indigo9", "red9")
57+
titles = ("Group 1", "Group 2")
58+
for ax, which, color, title in zip(axs, "lr", colors, titles):
59+
ax.hist2d(
60+
x,
61+
y,
62+
bins,
63+
vmin=0,
64+
vmax=10,
65+
levels=50,
66+
cmap=color,
67+
colorbar="b",
68+
colorbar_kw={"label": "count"},
69+
)
70+
color = pplt.scale_luminance(color, 1.5) # histogram colors
71+
px = ax.panel(which, space=0)
72+
px.histh(y, bins, color=color, fill=True, ec="k")
73+
px.format(grid=False, xlocator=[], xreverse=(which == "l"))
74+
px = ax.panel("t", space=0)
75+
px.hist(x, bins, color=color, fill=True, ec="k")
76+
px.format(grid=False, ylocator=[], title=title, titleloc="l")
77+
return fig

tests/test_subplots.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import proplot as pplt, pytest
2+
3+
4+
@pytest.mark.mpl_image_compare
5+
def test_stress_test_subplots():
6+
fig = pplt.figure(space=0, refwidth=0.7)
7+
axs = fig.subplots(nrows=8, ncols=8)
8+
axs.format(
9+
abc=True,
10+
abcloc="ur",
11+
xlabel="x axis",
12+
ylabel="y axis",
13+
xticks=[],
14+
yticks=[],
15+
suptitle="A-b-c label stress test",
16+
)
17+
return fig
18+
19+
20+
@pytest.mark.mpl_image_compare
21+
def test_subplot_variable_spacing():
22+
# Stress test of the tight layout algorithm
23+
# Add large labels along the edge of one subplot
24+
descrip = "variable"
25+
equal = False
26+
fig, axs = pplt.subplots(
27+
nrows=3, ncols=3, refwidth=1.1, share=False, equal=bool(equal)
28+
)
29+
axs[1].format(xlabel="xlabel\nxlabel", ylabel="ylabel\nylabel\nylabel\nylabel")
30+
axs.format(
31+
grid=False,
32+
toplabels=("Column 1", "Column 2", "Column 3"),
33+
leftlabels=("Row 1", "Row 2", "Row 3"),
34+
suptitle=f"Tight layout with {descrip} row-column spacing",
35+
)
36+
return fig
37+
38+
39+
@pytest.mark.mpl_image_compare
40+
def test_subplot_equal_spacing():
41+
# Stress test of the tight layout algorithm
42+
# Add large labels along the edge of one subplot
43+
descrip = "equal"
44+
equal = True
45+
fig, axs = pplt.subplots(
46+
nrows=3, ncols=3, refwidth=1.1, share=False, equal=bool(equal)
47+
)
48+
axs[1].format(xlabel="xlabel\nxlabel", ylabel="ylabel\nylabel\nylabel\nylabel")
49+
axs.format(
50+
grid=False,
51+
toplabels=("Column 1", "Column 2", "Column 3"),
52+
leftlabels=("Row 1", "Row 2", "Row 3"),
53+
suptitle=f"Tight layout with {descrip} row-column spacing",
54+
)
55+
return fig

0 commit comments

Comments
 (0)