Skip to content

Commit 53edcc1

Browse files
committed
Initialize mpl pytests
1 parent 62cf31a commit 53edcc1

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

tests/baseline/test_colorbar.png

322 KB
Loading

tests/baseline/test_inbounds_data.png

329 KB
Loading
416 KB
Loading
34.3 KB
Loading

tests/test_1d.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import proplot as plt, numpy as np, pytest
2+
from matplotlib.testing import setup
3+
4+
5+
@pytest.fixture()
6+
def setup_mpl():
7+
setup()
8+
plt.clf()
9+
10+
11+
@pytest.mark.mpl_image_compare
12+
def test_standardized_inputs_1d():
13+
N = 5
14+
state = np.random.RandomState(51423)
15+
with plt.rc.context({"axes.prop_cycle": plt.Cycle("Grays", N=N, left=0.3)}):
16+
# Sample data
17+
x = np.linspace(-5, 5, N)
18+
y = state.rand(N, 5)
19+
fig = plt.figure(
20+
share=False, suptitle="Standardized input demonstration", figsize=(5, 5)
21+
)
22+
23+
# Plot by passing both x and y coordinates
24+
ax = fig.subplot(121, title="Manual x coordinates")
25+
ax.area(x, -1 * y / N, stack=True)
26+
ax.bar(x, y, linewidth=0, alpha=1, width=0.8)
27+
ax.plot(x, y + 1, linewidth=2)
28+
ax.scatter(x, y + 2, marker="s", markersize=5**2)
29+
30+
# Plot by passing just y coordinates
31+
# Default x coordinates are inferred from DataFrame,
32+
# inferred from DataArray, or set to np.arange(0, y.shape[0])
33+
ax = fig.subplot(122, title="Auto x coordinates")
34+
ax.area(-1 * y / N, stack=True)
35+
ax.bar(y, linewidth=0, alpha=1)
36+
ax.plot(y + 1, linewidth=2)
37+
ax.scatter(y + 2, marker="s", markersize=5**2)
38+
fig.format(xlabel="xlabel", ylabel="ylabel")
39+
return fig
40+
41+
42+
if __name__ == "__main__":
43+
for func in [
44+
test_standardized_inputs_1d,
45+
]:
46+
func().savefig(f"./tests/baseline/{func.__name__}.png")

tests/test_imshow.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import pytest
2+
3+
import proplot as plt, numpy as np
4+
from matplotlib.testing import setup
5+
6+
7+
@pytest.fixture()
8+
def setup_mpl():
9+
setup()
10+
plt.clf()
11+
12+
13+
@pytest.mark.mpl_image_compare
14+
def test_standardized_input():
15+
# Sample data
16+
state = np.random.RandomState(51423)
17+
x = y = np.array([-10, -5, 0, 5, 10])
18+
xedges = plt.edges(x)
19+
yedges = plt.edges(y)
20+
data = state.rand(y.size, x.size) # "center" coordinates
21+
lim = (np.min(xedges), np.max(xedges))
22+
23+
with plt.rc.context({"cmap": "Grays", "cmap.levels": 21}):
24+
# Figure
25+
fig = plt.figure(refwidth=2.3, share=False)
26+
axs = fig.subplots(ncols=2, nrows=2)
27+
axs.format(
28+
xlabel="xlabel",
29+
ylabel="ylabel",
30+
xlim=lim,
31+
ylim=lim,
32+
xlocator=5,
33+
ylocator=5,
34+
suptitle="Standardized input demonstration",
35+
toplabels=("Coordinate centers", "Coordinate edges"),
36+
)
37+
38+
# Plot using both centers and edges as coordinates
39+
axs[0].pcolormesh(x, y, data)
40+
axs[1].pcolormesh(xedges, yedges, data)
41+
axs[2].contourf(x, y, data)
42+
axs[3].contourf(xedges, yedges, data)
43+
fig.show()
44+
return fig
45+
46+
47+
@pytest.mark.mpl_image_compare
48+
def test_inbounds_data():
49+
# Sample data
50+
cmap = "turku_r"
51+
state = np.random.RandomState(51423)
52+
N = 80
53+
x = y = np.arange(N + 1)
54+
data = 10 + (state.normal(0, 3, size=(N, N))).cumsum(axis=0).cumsum(axis=1)
55+
xlim = ylim = (0, 25)
56+
57+
# Plot the data
58+
fig, axs = plt.subplots(
59+
[[0, 1, 1, 0], [2, 2, 3, 3]],
60+
wratios=(1.3, 1, 1, 1.3),
61+
span=False,
62+
refwidth=2.2,
63+
)
64+
axs[0].fill_between(
65+
xlim,
66+
*ylim,
67+
zorder=3,
68+
edgecolor="red",
69+
facecolor=plt.set_alpha("red", 0.2),
70+
)
71+
for i, ax in enumerate(axs):
72+
inbounds = i == 1
73+
title = f"Restricted lims inbounds={inbounds}"
74+
title += " (default)" if inbounds else ""
75+
ax.format(
76+
xlim=(None if i == 0 else xlim),
77+
ylim=(None if i == 0 else ylim),
78+
title=("Default axis limits" if i == 0 else title),
79+
)
80+
ax.pcolor(x, y, data, cmap=cmap, inbounds=inbounds)
81+
fig.format(
82+
xlabel="xlabel",
83+
ylabel="ylabel",
84+
suptitle="Default vmin/vmax restricted to in-bounds data",
85+
)
86+
fig.show()
87+
return fig
88+
89+
90+
@pytest.mark.mpl_image_compare
91+
def test_colorbar():
92+
# Sample data
93+
state = np.random.RandomState(51423)
94+
data = 10 + state.normal(0, 1, size=(33, 33)).cumsum(axis=0).cumsum(axis=1)
95+
96+
# Figure
97+
fig, axs = plt.subplots([[1, 1, 2, 2], [0, 3, 3, 0]], ref=3, refwidth=2.3)
98+
axs.format(yformatter="none", suptitle="Discrete vs. smooth colormap levels")
99+
100+
# Pcolor
101+
axs[0].pcolor(data, cmap="viridis", colorbar="l")
102+
axs[0].set_title("Pcolor plot\ndiscrete=True (default)")
103+
axs[1].pcolor(data, discrete=False, cmap="viridis", colorbar="r")
104+
axs[1].set_title("Pcolor plot\ndiscrete=False")
105+
106+
# Imshow
107+
m = axs[2].imshow(data, cmap="oslo", colorbar="b")
108+
axs[2].format(title="Imshow plot\ndiscrete=False (default)", yformatter="auto")
109+
fig.show()
110+
return fig
111+
112+
113+
if __name__ == "__main__":
114+
for func in [
115+
test_standardized_input,
116+
test_inbounds_data,
117+
test_colorbar,
118+
]:
119+
fig = func()
120+
fig.savefig(f"./tests/baseline/{func.__name__}.png")
121+
plt.show(block=1)

0 commit comments

Comments
 (0)