-
Notifications
You must be signed in to change notification settings - Fork 19
/
test_pyplot.py
137 lines (104 loc) · 3.48 KB
/
test_pyplot.py
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
import matplotlib.pyplot as plt
import numpy as np
import pytest
from matplotlib import __version__ as mpl_version
from packaging import version
import mpl_interactions.ipyplot as iplt
from mpl_interactions.pyplot import interactive_hist, interactive_plot
from mpl_interactions.utils import figure
np.random.seed(1111111121)
mpl_gr_32 = version.parse(mpl_version) >= version.parse("3.3")
def f_hist(loc, scale):
return np.random.randn(1000) * scale + loc
@pytest.mark.mpl_image_compare(style="default")
def test_hist_plot():
fig = figure(2)
ax = fig.gca()
controls = interactive_hist(f_hist, density=True, loc=(5.5, 100), scale=(10, 15))
fig.tight_layout()
return fig
@pytest.mark.mpl_image_compare(style="default")
def test_hist_controls():
if not mpl_gr_32:
pytest.skip("wonky font differences")
fig, ax = plt.subplots()
controls = interactive_hist(
f_hist, density=True, loc=(5.5, 100), scale=(10, 15), slider_formats="{:.1f}"
)
return controls.control_figures[0]
def f1(x, tau, beta):
return np.sin(x * tau) * x * beta
def f2(x, tau, beta):
return np.sin(x * beta) * x * tau
@pytest.mark.mpl_image_compare(style="default")
def test_mixed_types():
if not mpl_gr_32:
pytest.skip("wonky font differences")
def foo(x, **kwargs):
return x
x = np.linspace(0, 1)
a = np.linspace(0, 10)
b = (0, 10, 15)
# set order is determined in part by PYTHONHASHSEED
# but there doesn't seem to be an easy way to set this for pytest
# so the unordered set will change its order from test to test :/
# c = {'this', 'set will be', 'unordered'}
d = {("this", "set will be", "ordered")}
e = 0 # this will not get a slider
# can't test ipywidgets yet
# no mpl widgets for booleans
# f = widgets.Checkbox(value=True, description='A checkbox!!')
return interactive_plot(x, foo, a=a, b=b, d=d, e=e, display=False).control_figures[0]
def f1(x, tau, beta):
return np.sin(x * tau) * x * beta
def f2(x, tau, beta):
return np.sin(x * beta) * x * tau
x = np.linspace(0, np.pi, 100)
tau = (5, 10, 100)
beta = (1, 2)
@pytest.mark.mpl_image_compare(style="default")
def test_multiple_functions():
fig, ax = plt.subplots()
controls = interactive_plot(x, f1, tau=tau, beta=beta, label="f1")
interactive_plot(x, f2, controls=controls, label="f2")
_ = plt.legend()
return fig
@pytest.mark.mpl_image_compare(style="default")
def test_styling():
fig, ax = plt.subplots()
controls = interactive_plot(
x,
f1,
beta=beta,
tau=tau,
label="f1",
)
iplt.title("the value of tau is: {tau:.2f}", controls=controls["tau"])
interactive_plot(
x,
f2,
controls=controls,
label="custom label!",
linestyle="--",
)
plt.legend()
return fig
def test_imshow_scalars():
# and by proxy all the scalar handling
def mask(min_distance):
return np.random.randn(10, 10)
fig, ax = plt.subplots()
iplt.imshow(mask, min_distance=(1, 10), alpha=(0, 1))
def test_title():
fig, ax = plt.subplots()
ctrls = iplt.plot(1, 1, E=3e7)
expected = "E=3.00e+07"
iplt.title("E={E:.2e}", controls=ctrls)
assert not isinstance(ctrls.params["E"], np.ndarray)
assert ax.get_title() == expected
plt.close()
fig, ax = plt.subplots()
ctrls = iplt.plot(1, 1, E=np.array([3e7, 3e9]))
iplt.title("E={E:.2e}", controls=ctrls)
assert ax.get_title() == expected
plt.close()