Skip to content

Commit ccb935d

Browse files
committed
add tests for colorbar/norm
1 parent 51e90cd commit ccb935d

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

src/matplotgl/mesh.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, *args, cmap: str = "viridis", norm: str = "linear"):
2727
y = np.arange(M + 1)
2828

2929
self.axes = None
30+
self._colorbar = None
3031
self._xscale = "linear"
3132
self._yscale = "linear"
3233

tests/colorbar_test.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
3+
import matplotlib.colors as cm
4+
import numpy as np
5+
6+
import matplotgl.pyplot as plt
7+
8+
9+
def test_colorbar_imshow():
10+
fig, ax = plt.subplots()
11+
12+
a = np.random.random((20, 20)) * 5.0
13+
im = ax.imshow(a)
14+
cb = fig.colorbar(im)
15+
16+
assert cb._mappable is im
17+
assert im._colorbar is not None
18+
assert isinstance(im.norm._norm, cm.Normalize)
19+
assert im.norm.vmin == a.min()
20+
assert im.norm.vmax == a.max()
21+
22+
23+
def test_colorbar_imshow_log():
24+
fig, ax = plt.subplots()
25+
26+
a = np.random.random((20, 20)) * 100.0 + 1.0
27+
im = ax.imshow(a, norm='log')
28+
cb = fig.colorbar(im)
29+
30+
assert cb._mappable is im
31+
assert im._colorbar is not None
32+
assert isinstance(im.norm._norm, cm.LogNorm)
33+
assert im.norm.vmin == a.min()
34+
assert im.norm.vmax == a.max()
35+
36+
37+
def test_imshow_set_cmap():
38+
fig, ax = plt.subplots()
39+
40+
a = np.random.random((20, 20)) * 5.0
41+
im = ax.imshow(a)
42+
fig.colorbar(im)
43+
44+
im.set_cmap('plasma')
45+
im.cmap = 'magma'
46+
47+
48+
def test_imshow_set_norm():
49+
fig, ax = plt.subplots()
50+
51+
a = np.random.random((20, 20)) * 100.0 + 1.0
52+
im = ax.imshow(a, norm='linear')
53+
fig.colorbar(im)
54+
55+
im.norm = 'log'
56+
assert isinstance(im.norm._norm, cm.LogNorm)
57+
58+
59+
def test_colorbar_pcolormesh():
60+
fig, ax = plt.subplots()
61+
62+
a = np.random.random((20, 20)) * 5.0
63+
im = ax.pcolormesh(a)
64+
cb = fig.colorbar(im)
65+
66+
assert cb._mappable is im
67+
assert im._colorbar is not None
68+
assert isinstance(im.norm._norm, cm.Normalize)
69+
assert im.norm.vmin == a.min()
70+
assert im.norm.vmax == a.max()
71+
72+
73+
def test_colorbar_pcolormesh_log():
74+
fig, ax = plt.subplots()
75+
76+
a = np.random.random((20, 20)) * 100.0 + 1.0
77+
im = ax.pcolormesh(a, norm='log')
78+
cb = fig.colorbar(im)
79+
80+
assert cb._mappable is im
81+
assert im._colorbar is not None
82+
assert isinstance(im.norm._norm, cm.LogNorm)
83+
assert im.norm.vmin == a.min()
84+
assert im.norm.vmax == a.max()
85+
86+
87+
def test_pcolormesh_set_cmap():
88+
fig, ax = plt.subplots()
89+
90+
a = np.random.random((20, 20)) * 5.0
91+
im = ax.pcolormesh(a)
92+
fig.colorbar(im)
93+
94+
im.set_cmap('plasma')
95+
im.cmap = 'magma'
96+
97+
98+
def test_pcolormesh_set_norm():
99+
fig, ax = plt.subplots()
100+
101+
a = np.random.random((20, 20)) * 100.0 + 1.0
102+
im = ax.pcolormesh(a, norm='linear')
103+
fig.colorbar(im)
104+
105+
im.norm = 'log'
106+
assert isinstance(im.norm._norm, cm.LogNorm)
107+
108+
109+
def test_colorbar_scatter():
110+
fig, ax = plt.subplots()
111+
112+
x = np.random.random(100)
113+
y = np.random.random(100)
114+
c = np.random.random(100) * 5.0
115+
im = ax.scatter(x, y, c=c)
116+
cb = fig.colorbar(im)
117+
118+
assert cb._mappable is im
119+
assert im._colorbar is not None
120+
assert isinstance(im.norm._norm, cm.Normalize)
121+
assert im.norm.vmin == c.min()
122+
assert im.norm.vmax == c.max()
123+
124+
125+
def test_colorbar_scatter_log():
126+
fig, ax = plt.subplots()
127+
128+
x = np.random.random(100)
129+
y = np.random.random(100)
130+
c = np.random.random(100) * 100.0 + 1.0
131+
im = ax.scatter(x, y, c=c, norm='log')
132+
cb = fig.colorbar(im)
133+
134+
assert cb._mappable is im
135+
assert im._colorbar is not None
136+
assert isinstance(im.norm._norm, cm.LogNorm)
137+
assert im.norm.vmin == c.min()
138+
assert im.norm.vmax == c.max()
139+
140+
141+
def test_scatter_set_cmap():
142+
fig, ax = plt.subplots()
143+
144+
x = np.random.random(100)
145+
y = np.random.random(100)
146+
c = np.random.random(100) * 5.0
147+
im = ax.scatter(x, y, c=c)
148+
fig.colorbar(im)
149+
150+
im.set_cmap('plasma')
151+
im.cmap = 'magma'
152+
153+
154+
def test_scatter_set_norm():
155+
fig, ax = plt.subplots()
156+
157+
x = np.random.random(100)
158+
y = np.random.random(100)
159+
c = np.random.random(100) * 100.0 + 1.0
160+
im = ax.scatter(x, y, c=c, norm='linear')
161+
fig.colorbar(im)
162+
163+
im.norm = 'log'
164+
assert isinstance(im.norm._norm, cm.LogNorm)

tests/plot_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
13
import numpy as np
24

35
import matplotgl.pyplot as plt

0 commit comments

Comments
 (0)